现在的位置: 首页 > web前端 > 正文

JavaScript Prototype in Plain Language

2018年09月30日 web前端 ⁄ 共 2672字 ⁄ 字号 评论关闭

原文地址

http://javascriptissexy.com/javascript-prototype-in-plain-detailed-language/

Prototype相关概念

1. prototype property.  (看作为对象的成员,类成员)

每个JavaScript函数(默认为空),当要实现继承的时候可以通过在这个prototype property上添加相关属性和方法。这个prototype property不能够被枚举(指prototype本身并非其上附属的属性和方法),不能使用for/in循环。prototype property主要用于继承,用户可以添加成员和方法到一个函数的prototype property上,这样的话函数的实例可以访问这些成员和方法。

function PrintStuff (myDocuments) {
	this.documents = myDocuments;
	this.prototype=12;
}
// We add the print () method to PrintStuff prototype property 
//so that other instances (objects) can inherit it:
PrintStuff.prototype.print = function () {
	alert(this.documents);
}

// Create a new object with the PrintStuff () constructor, 
//thus allowing this new object to inherit PrintStuff's properties and methods.
var newObj = new PrintStuff ("I am a new Object and I can print.");
newObj.print (); //I am a new Object and I can print.

2. prototype attribute. (看做对象的父亲)

prototype attribute可以看做用于描述对象的特征,这些特征告诉我们对象的父亲parent。简单来说一个对象的prototype attribute指向该对象的父亲即该对象所继承的属性来源。prototype attribute通常来说是指prototype object,当创建对象的时候其被自动设置。上述例子中newObje的prototype是PrintStuff.prototype

// The userAccount object inherits from Object 
//and as such its prototype attribute is Object.prototype.
var userAccount = new Object ();
// This demonstrates the use of an object literal to create userAccount object;
//the userAccount object inherits from Object; 
//therefore, its prototype attribute is Object.prototype.
var userAccount = {name: “Mike”} 
function Account () {}
//userAccount initialized with the Account () constructor and
//as such its prototype attribute(or prototype object) is Account.prototype.
var userAccount = new Account () 

(1) 若对象通过字面量方式创建(obj={}),则其属性继承自Object.prototype,其prototype attribute为Object.prototype

(2)若对象通过构造器构建如(new Object(),new Fruit(),new Array()等),则其继承自改构造函数(prototype attribute为Object.prototype,Fruit.prototype和Array.prototype)

prototype property和prototype attribute实质都是拥有描述javascript中基于prototype的继承机制。

prototype properties从如何实现继承角度进行描述,由于JS中没有引入class,采用原型机制来实现类属性和方法继承机制,其中function就和class类似,每个function拥有一个prototype对象,通过在prototype上附加属性和方法使得其实例具有相应的属性和方法。

prototype attribute是从对象本身出发,如何通过prototype追溯其源头即父亲或祖先,包括对象是如何获取其属性和方法,JS中所有的对象都继承了Object.prototype。

Prototype的重要性和用途

1. Prototype Property: Prototype-based Inheritance

JavaScript中没有类继承机制,但有基于原型的继承机制,通过Prototype Property来实现继承。

2. Prototype Attribute: Accessing Properties on Objects

原型对于访问对象的成员和方法同样至关重要。Prototype Attribute描述了对象最初定义其相关成员和方法的来源。

prototype chain:当访问对象属性或方法时,先直接搜索其自身是否包含,若没有则到对象的原型上搜索,若还是没有找到,则到原型对象的原型继续搜索直至搜索到Object.prototype停止。

var myFriends = {name: "Pete"};
//直接找到myFriends对象的name属性
console.log(myFriends.name);
//从myFriends对象开始,然后搜索到Object.prototype
myFriends.toString ();

Object.prototype被所有对象继承,继承的属性和方法包括如下:

constructor,hasOwnProperty(),isPrototypeOf(),propertyIsEnumerable(), toLocaleString(),toString(),valueOf(). 

实际上Object.prototype本身是Object,其不再继承任何类

抱歉!评论已关闭.