构造器模式

Constructor Pattern

知识点一:有四种方法可以把键值对赋值给一个对象

//ECMAScript 3 
//1. 点语法
// Set properties
newObject.someKey = "Hello World";

// Get properties
var value = newObject.someKey;

// 2. 中括号访问

// Set properties
newObject["someKey"] = "Hello World";

// Get properties
var value = newObject["someKey"];

//ECMAScript 5

// 3. Object.defineProperty
// Set properties
Object.defineProperty( newObject, "someKey", {
    value: "for more control of the property's behavior",
    writable: true,
    enumerable: true,
    configurable: true
});

// 4. Object.defineProperties
// Set properties
Object.defineProperties( newObject, {

  "someKey": {
    value: "Hello World",
    writable: true
  },

  "anotherKey": {
    value: "Foo bar",
    writable: false
  }

});

知识点二:简单的构建模式

知识点三:带原型的构建模式

Last updated

Was this helpful?