// 建立对象,赋给属性 var person = new Object(); person.name = "Nicholas"; person.age = 29; person.job = "Sofware Engineer"; person.sayName = function() { alert(this.name); } // 字面量方式建立对象 var person = { name: "Nicholas", age: 29, job: "Sofware Engineer", sayName: function() { alert(this.name); } }
数据属性包含一个数值的位置。在这个位置能够读取和写入值。数据属性有4个描述其行为的特性javascript
要修改属性默认的特性,必须使用ECMAScript5的Object.defineProperty()方法。java
var person = {}; // 建立了一个name属性,它的值"Nicholas"是只读的。 Object.defineProperty(person, "name", { writable: false, value: "Nicholas" }); console.log(person.name); // "Nicholas" // 在非严格模式下,赋值操做会被忽略 // 而在严格模式下,会致使错误 person.name = "Greg"; console.log(person.name); // "Nicholas"
var person = {}; Object.defineProperty(person, "name", { configurable: false, value: "Nicholas" }); // 抛出错误 Object.defineProperty(person, "name", { configurable: true, value: "Nicholas" })
访问器属性有以下4个特性:数组
var book = { _year: 2004, edition: 1 }; Object.defineProperty(book, "year", { get: function () { return this._year; }, set: function (newValue) { if (newValue > 2004) { this._year = newValue; this.edition += newValue - 2004; } } }); // 这是使用访问器属性的常见方式,即设置一个属性的值会致使其余属性发生变化。 book.year = 2005; console.log(book.edition); // 2
var book = {} Object.defineProperties(book, { _year: { writable: true, value: 2004 }, edition: { writable: true, value: 1 }, year: { get: function() { return this._year; }, set: function(newValue) { if (newValue > 2004) { this._year = newValue; this.editio += newValue - 2004; } } } })
var book = {}; Object.defineProperties(book, { _year: { writable: true, value: 2004 }, edition: { writable: true, value: 1 }, year: { get: function() { return this._year; }, set: function(newValue) { if (newValue > 2004) { this._year = newValue; this.editio += newValue - 2004; } } } }); var descriptor = Object.getOwnPropertyDescriptor(book, "_year"); console.log(descriptor.value); // 2004 console.log(descriptor.configurable); // false console.log(typeof descriptor.get); // undefined var descriptor = Object.getOwnPropertyDescriptor(book, "year"); console.log(descriptor.value); // undefined console.log(descriptor.enumerable); // false console.log(typeof descriptor.get); // "function"
function createPerson(name, age, job) { var o = new Object(); o.name = name; o.age = age; o.job = job; o.sayName = function () { alert(this.name); }; return o; } var person1 = createPerson("Nicholas", 29, "Software Engineer"); var person2 = createPerson("Greg", 27, "Doctor");
function Person(name, age, job) { this.name = name; this.age = age; this.job = job; this.sayName = function () { alert(this.name); }; } var person1 = new Person("Nicholas", 29, "Software Engineer"); var person2 = new Person("Greg", 27, "Doctor");
构造函数模式对比工厂模式存在如下不一样之处浏览器
console.log(person1.constructor == Person); // true console.log(person2.constructor == Person); // true
console.log(person1 instanceof Object); // true console.log(person2 instanceof Object); // true console.log(person1 instanceof Person); // true console.log(person2 instanceof Person); // true
// 做为构造函数使用 var person = new Person("Nicholas", 29, "Software Engineer"); person.sayName(); // "Nicholas" // 做为普通函数调用,此时this指向window对象 Person("Greg", 27, "Doctor"); // 添加到window对象上 window.sayName(); // "Greg" // 在另外一个对象的做用域中调用 var o = new Object(); Person.call(o, "Kristen", 25, "Nurse"); o.sayName(); // "Kristen"
function Person(name, age, job) { this.name = name; this.age = age; this.job = job; // 与声明函数再路基上是等价的 this.sayName = new Function("alert(this.name)"); } console.log(person1.sayName == person2.sayName); // false
function Person(name, age, job) { this.name = name; this.age = age; this.job = job; this.sayName = sayName } function sayName () { alert(this.name); } var person1 = new Person("Nicholas", 29, "Software Engineer"); var person2 = new Person("Greg", 27, "Doctor");
function Person() {} Person.prototype.name = "Nicholas"; Person.prototype.age = 29; Person.prototype.job = "Software Engineer"; Person.prototype.sayName = function () { alert(this.name); }; var person1 = new Person(); person1.sayName(); // "Nicholas" var person2 = new Person(); person2.sayName(); // "Nicholas" console.log(person1.sayName == person2.sayName); // true
console.log(Person.prototype.isPrototypeOf(person1)); // true console.log(Person.prototype.isPrototypeOf(person2)); // true console.log(Person.isPrototypeOf(person1)); // false console.log(Person.isPrototypeOf(person2)); // false
console.log(Object.getPrototypeOf(person1) == Person.prototype); // true console.log(Object.getPrototypeOf(person1).name); // "Nicholas"
function Person() {} Person.prototype.name = "Nicholas"; Person.prototype.age = 29; Person.prototype.job = "Software Engineer"; Person.prototype.sayName = function () { alert(this.name); }; var person1 = new Person(); var person2 = new Person(); console.log(person1.hasOwnProperty("name")); // false person1.name = "Greg"; console.log(person1.name); // "Greg" ——来自实例 console.log(person1.hasOwnProperty("name")); // true console.log(person2.name); // "Nicholas" ——来自原型 console.log(person2.hasOwnProperty("name")); // false // 使用delete操做符删除实例中的属性 delete person1.name; console.log(person1.name); // "Nicholas" ——来自实例 console.log(person1.hasOwnProperty("name")); // false
function Person() {} Person.prototype.name = "Nicholas"; Person.prototype.age = 29; Person.prototype.job = "Software Engineer"; Person.prototype.sayName = function () { alert(this.name); }; var person1 = new Person(); var person2 = new Person(); console.log(person1.hasOwnProperty("name")); // false console.log("name" in person1); // true person1.name = "Greg"; console.log("name" in person1); // true console.log(person2.name); // "Nicholas" ——来自原型 console.log("name" in person2); // true // 使用delete操做符删除实例中的属性 delete person1.name; console.log("name" in person1); // true
function hasPrototypeProperty(object, name) { return !object.hasOwnProperty(name) && (name in object); }
function Person() {} Person.prototype.name = "Nicholas"; Person.prototype.age = 29; Person.prototype.job = "Software Engineer"; Person.prototype.sayName = function () { alert(this.name); }; var person = new Person(); console.log(hasPrototypeProperty(person, "name")); // true person.name = "Greg"; console.log(hasPrototypeProperty(person, "name")); // false
var o = { toString: function() { return "My Object"; } }; for (var prop in o) { if (prop == "toString") { console.log("Found toString"); // 在IE中不会显示 } }
function Person() {} Person.prototype.name = "Nicholas"; Person.prototype.age = 29; Person.prototype.job = "SoftWare Engineer"; Person.prototype.sayName = function() { console.log(this.name); }; var keys = object.keys(Person.prototype); console.log(keys); // "name,age,job,sayName" var p1 = new Person(); p1.name = "Rob"; p1.age = 31; var p1kyes = Object.keys(p1); console.log(p1kyes); // "name,age"
var keys = Object.getOwnPropertyNames(Person.prototype); // "constructor,name,age,job,sayName"
function Person() {} Person.prototype = { name: "Nicholas", age: 29, job: "Software Engineer", sayName: function () { console.log(this.name); } };
var friend = new Person(); console.log(friend instanceof Object); // true console.log(friend instanceof Person); // true console.log(friend.constructor == Person); // false console.log(friend.constructor == Object); // Object
function Person() {} Person.prototype = { constructor: Person, // 让 prototype的constructor从新指向Person name: "Nicholas", age: 29, job: "Software Engineer", sayName: function () { console.log(this.name); } };
function Person() {} Person.prototype = { name: "Nicholas", age: 29, job: "Software Engineer", sayName: function () { console.log(this.name); } }; // 重设构造函数,只适用于ECMASCript 5 兼容的浏览器 Object.defineProperty(Person.prototype, "constructor", { enumerable: false, value: Person });
var friend = new Person(); Person.prototype.sayHi = function() { console.log("Hi"); }; friend.sayHi(); // "Hi"
function Person() {} var friend = new Person(); // 重写整个原型对象,就等于切断了构造函数与最初原型之间的联系 Person.prototype = { constructor: Person, age: 29, job: "Software Engineer", sayName: function () { console.log(this.name); } }; friend.sayName(); // error
console.log(typeof Array.prototype.sort); // "function" console.log(typeof String.prototype.substring); // "function"
String.prototype.startsWith = function (text) { return this.indexOf(text) == 0; }; var msg = "Hello world!"; console.log(msg.startsWith("Hello")); // true
原型模式也不是没有缺点。安全
function Person() {} Person.prototype = { constructor: Person, // 让 prototype的constructor从新指向Person name: "Nicholas", age: 29, job: "Software Engineer", friend: ["Shelby", "Court"], sayName: function () { console.log(this.name); } }; var person1 = new Person(); var person2 = new Person(); // 这里修改的其实是Person.prototype.friends person1.friends.push("Van"); // 不但person1的friends属性被修改,person2也作了一样的改动 console.log(person1.friends); // "Shelby,Court,Van" console.log(person1.friends); // "Shelby,Court,Van" // 由于两个实例的friends属性指向的都是Person.prototype.friends console.log(person1.friends === person2.friends); // true
function Person(name, age, job) { this.name = name; this.age = age; this.job = job; this.friends = ["Shelby", "Court"]; } Person.prototype = { constructor: Person, sayName: function() { console.log(this.name); } }; var person1 = new Person("Nicholas", 29, "Software Engineer"); var person2 = new Person("Greg", 27, "Doctor"); person1.friends.push("Van"); console.log(person1.friends); // "Shelby,Court,Van" console.log(person1.friends); // "Shelby,Court" console.log(person1.friends === person2.friends); // false console.log(person1.sayName === person2.sayName); // true
function Person(name, age, job) { // 属性 this.name = name; this.age = age; this.job = job; // 方法 if (typeof this.sayName != "function") { Person.perototype.sayName = function() { console.log(this.name); }; } } var friend = new Person("Nicholas", 29, "Software Engineer"); friend.sayName(); // "Nicholas"
function Person(name, age, job) { var o = new Object(); o.name = name; o.age = age; o.job = job; o.sayName = function() { console.log(this.name); }; return o } var friend = new Person("Nicholas", 29, "Software Engineer"); friend.sayName(); // "Nicholas"
function SpecialArray() { // 建立数组 var values = new Array(); // 添加值 values.push.apply(values, arguments); // 添加方法 values.toPipedString = function() { return this.join("|"); }; // 返回数组 return values; } var colors = new SpecialArray("red", "blue", "green"); console.log(colors.toPipedString()); // "red|blue|green"
道格拉斯·克罗克福德(Douglas Crockford)发明了JavaScript中的稳妥对象(durable objects)这个概念。稳妥对象,指的是没有公共属性并且其方法不引用this的对象。稳妥对象最适合在一些安全的环境中(这些环境中会禁止使用this和new),或者在防止数据被其余应用程序(如Mashup程序)改动时使用。稳妥构造函数遵循与寄生构造函数相似的模式,但有两点不一样:app
this
function Person(name, age, job) { // 建立要返回的对象 var o = new Object(); // 这里定义私有变量和函数 ... // 添加方法 o.sayName = function() { console.log(name); }; // 返回对象 return o; } var friend = Person("Nicholas", 29, "Software Engineer"); friend.sayName(); // "Nicholas"
许多OO语言都支持两种继承方式函数
原型链 实现继承的主要方法。基本思想是利用原型让一个引用类型继承另外一个引用类型的属性和方法。简单回顾一下构造函数、原型和实例的关系:测试
function SuperType() { this.property = true; } SuperType.prototype.getSuperValue = function() { return this.property; }; function SubType() { this.subproperty = false; } // 继承了SuperType SubType.prototype = new SuperType(); SubType.prototype.getSubValue = function () { return this.subproperty; }; var instance = new SubType(); console.log(instance.getSuperValue()); //true
最终:this
经过实现原型链,本质上拓展了原型搜索机制。当读取模式访问一个实例属性时,首先会在实例中搜索该属性。若是没有找到该属性,则会继续搜索实例的原型。在经过原型链实现继承的状况下,搜索过程就得以沿着原型链继续向上。调用instance.getSuperValue()会经历三个搜索步骤spa
// 因为原型链,咱们能够是instance是Object,SuperType, SubType中任何一个类型的实例 console.log(instance instanceof Object); // true console.log(instance instanceof SuperType); // true console.log(instance instanceof SubType); // true
console.log(Object.prototype.isPrototypeOf(instance)); // true console.log(SuperType.prototype.isPrototypeOf(instance)); // true console.log(SubType.prototype.isPrototypeOf(instance)); // true
function SuperType() { this.property = true; } SuperType.prototype.getSuperValue = function() { return this.property; }; function SubType() { this.subproperty = false; } // 继承了SuperType,原来默认的原型被替换 SubType.protoype = new SuperType(); // 添加新方法 SubType.prototype.getSubValue = function () { return this.subproperty; }; // 重写超类型中的方法 SubType.protoype.getSuperValue = function () { return false; }; var instance = new SubType(); console.log(instance.getSuperValue()); // false
function SuperType() { this.property = true; } SuperType.prototype. getSuperValue = function() { return this.property; }; function SubType() { this.subproperty = false; } // 继承了SuperType SubType.protype = new SuperType(); // 使用字面量添加新方法,会致使上一行代码无效 // 原型链被切断——SubType 和 SuperType 之间已经没有关系 SubType.prototype = { getSubValue: function () { return this.subproperty; }, someOtherMethod: function () { return false; } }; var instance = new SubType(); console.log(instance.getSuperValue())); // error
function SuperType() { this.colors = ["red", "blue", "green"]; } function SubType() { } // SubType 继承了 SuperType 以后 // SubType.prototype 就变成了 SuperType的一个实例 // 所以 SubType.prototype 也拥有了本身的colors属性 等价于建立了一个SubType.prototype.colors SubType.protype = new SuperType(); var instance1 = new SubType(); instance1.colors.push("black"); console.log(instance1.colors); // "red,blue,green,black" // 结果就是全部SubType实例都会共享这个colors属性 var instance2 = new SubType(); console.log(instance2.colors); // "red,blue,green,black"
function SuperType() { this.colors = ["red", "blue", "green"]; } function SubType() { // 继承了SuperType // "借调“了超类型的构造函数 SuperType.call(this); } var instance1 = new SubType(); instance1.colors.push("black"); console.log(instance1.colors); // "red,blue,green,black" // SubType实例都不会共享这个colors属性 var instance2 = new SubType(); console.log(instance2.colors); // "red,blue,green"
function SuperType(name) { this.name = name; } function SubType() { // 继承了SuperType 同时传递了参数 SuperType.call(this. "Nicholas"); // 实例属性 this.age = 29; } var instance = new SubType(); console.log(instance.anme); // "Nicholas" console.log(instance.age); // 29
function SuperType(name) { this.name = name; this.colors = ["red", "blue", "green"]; } SuperType.prototype.sayName = function() { console.log(this.name); } function SubType(name, age) { // 继承属性 SuperType.call(this, name); // 子类型本身的属性 this.age = age } // 继承方法 SubType.prototype = new SuperType(); // 若是不指定constructor,SubType.prototype.constructor 为 SuperType SubType.prototype.constructor = SubType; SubType.prototype.sayAge = function() { console.log(this.age); }; var instance1 = new SubType("Nicholas", 29); instance1.colors.push("black"); console.log(instance1.colors); // "red,blue,green,black" instance1.sayName(); // "Nicholas" instance1.sayAge(); // 29 var instance2 = new SubType("Greg", 27); console.log(instance2.colors); // "red,blue,green" instance2.sayName(); // "Greg" instance2.sayAge(); // 27
function object(o) { function F() {} F.prototype = o; return new F(); }
var person = { name: "Nicholas", friends: ["Shelby", "Court", "Van"], }; var anotherPerson = object(person); anotherPerson.name = "Greg"; anotherPerson.friends.push("Rob"); console.log(anotherPerson.friends); // "Shelby,Court,Van,Rob" var yetAnotherPerson = object(person); yetAnotherPerson.name = "Linda"; yetAnotherPerson.friends.push("Barbie"); console.log(yetAnotherPerson.friends); // "Shelby,Court,Van,Rob,Barbie" console.log(person.friends); // "Shelby,Court,Van,Rob,Barbie"
var person = { name: "Nicholoas", friends: ["Shelby", "Court", "Van"] }; var anotherPerson = Object.create(person); anotherPerson.name = "Greg"; anotherPerson.friends.push("Rob"); console.log(anotherPerson.friends); // "Shelby,Court,Van,Rob" var yetAnotherPerson = Object.object(person); yetAnotherPerson.name = "Linda"; yetAnotherPerson.friends.push("Barbie"); console.log(yetAnotherPerson.friends); // "Shelby,Court,Van,Rob,Barbie" console.log(person.friends); // "Shelby,Court,Van,Rob,Barbie"
var person = { name: "Nicholoas", friends: ["Shelby", "Court", "Van"] }; var anotherPerson = Object.create(person, { name: { value: "Greg" } }); console.log(anotherPerson.name); // "Greg"
function createAnother(original) { var clone = object(original); // 经过调用函数建立一个新对象 clone.sayHi = function() { // 以某种方式加强这个对象 console.log("hi"); }; return clone; // 返回这个对象 }
组合继承是最经常使用的继承模式;不过最大的问题就是不管什么状况下,都会调用两次超类型构造函数:
function SuperType(name) { this.name = name; this.colors = ["red", "blue", "green"]; } SuperType.prototype.sayName = function() { console.log(this.name); }; function SubType(name, age) { SuperType.call(this, name); // 第二次调用SuperType() this.age = age; } // 实例化SuperType做为SubType的原型 // 当即触发 SubType.prototype = new SuperType(); // 第一次调用SuperType() SubType.prototype.constructor = SubType; SubType.prototype.sayAge = function() { console.log(this.age); }; // 此时触发第二次调用 var instance = new SubType("Nicholas", 29); console.log(instance.name); // "Nicholas" console.log(SubType.prototype.name); // undefined
function inheritPrototype(subType, superType) { // 建立对象 - 超类型的对象原型的副本 // 这里没有使用new操做符,因此没有生成SuperType的实例 // 这里没有调用SuperType构造函数 var prototype = Object(superType.prototype) console.log(prototype == superType.prototype) // 加强对象 - 弥补因重写原型而失去的默认constructor属性 // 而这也将致使supert.prototype.constructor指向了subType prototype.constructor = subType; // 指定对象 - 将新建立的对象(即副本)赋值给子类型的原型。 subType.prototype = prototype; } function SuperType(name) { this.name = name; this.colors = ["red", "blue", "green"]; } SuperType.prototype.sayName = function() { console.log(this.name); }; function SubType(name, age) { SuperType.call(this, name); this.age = age; } inheritPrototype(SubType, SuperType); SubType.prototype.sayAge = function() { console.log(this.age); };