Javascript 設計模式 應用級講解

這裏寫圖片描述

這裏寫圖片描述

首先npm init一路回車 新建webpack.dev.config.js,修改package.json文件,安裝webpack-dev-server能夠幫助你在代碼發生變化後自動編譯代碼

這裏寫圖片描述

修改package.json文件javascript

"scripts": {
    "dev": "webpack-dev-server --config ./webpack.dev.config.js --mode development"
  },

webpack.dev.config.jshtml

const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
    entry: './src/index.js',
    output: {
        path: __dirname,
        filename: './release/bundle.js'  // release 會自動建立
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './index.html'  // bundle.js 會自動注入
        })
    ],
    devServer: {
        contentBase: path.join(__dirname, "./release"),  // 根目錄
        open: true,  // 自動打開瀏覽器
        port: 9000,   // 端口
        proxy: {
            '/api/*': {
                target: 'http://localhost:8880'
            }
        }
    },
    module: {
        rules: [{
            test: /\.js?$/,
            exclude: /(node_modules)/,
            loader: 'babel-loader'
        }]
    }
}

package.json 依據上面的方式,安裝如下依賴java

{
  "name": "es6",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "webpack-dev-server --config ./webpack.dev.config.js --mode development"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.4",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-polyfill": "^6.26.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-latest": "^6.24.1",
    "html-webpack-plugin": "^3.2.0",
    "webpack": "^4.8.3",
    "webpack-cli": "^2.1.3",
    "webpack-dev-server": "^3.1.4"
  },
  "dependencies": {
    "javascript-state-machine": "^3.0.1",
    "jquery": "^3.3.1"
  }
}
面向對象三要素:封裝、多態、繼承,子類繼承父類,封裝對數據的權限和保密,多態,同一個接口不一樣的實現,不從過後臺開發很難真正理解這幾個概念。
class People {
    constructor(name, age) {
        this.name = name
        this.age = age
    }
    eat() {
        alert(`${this.name} eat something`)
    }
    speak() {
        alert(`My name is ${this.name}, age ${this.age}`)
    }
}

class Student extends People {     //繼承關鍵字extends
    constructor(name, age, number) {
        super(name, age)  //super 傳給父類執行
        this.number = number
    }
    study() {
        alert(`${this.name} study`)
    }
}

let xiaoming = new Student('xiaoming', 10, 'A1')
xiaoming.study()
console.log(xiaoming.number)
let xiaohong = new Student('xiaohong', 11, 'A2')
xiaohong.study()

實際應用就是提取公共部分,複用代碼。node

關於封裝,public徹底開發、protected對子類開放、private對本身開放,目前es6還不支持。
相關文章
相關標籤/搜索