Last active
September 15, 2015 04:36
-
-
Save kenyonduan/dd0476bb853a748834a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// __proto__ | |
var array= [1,2,3] | |
array.push(4) //哪里来的这个方法? | |
//当访问对象其中的一个成员或方法的时候,如果这个对象中没有这个方法或成员, | |
//那么Javascript引擎将会访问这个对象的__proto__成员所指向的另外的一个对象,并在那个对象中查找指定的方法或成员, | |
//如果不能找到,那就会继续通过那个对象的__proto__成员指向的对象进行递归查找,直到这个链表结束 | |
//有点像 Ruby 中得方法查找 | |
//因为 chrome 中访问不到 __proto__ 这个属性 | |
[] instanceof Array // => true | |
Object.getOwnPropertyNames(Array) //=> ["length", "name", "arguments", "caller", "prototype", "isArray", "observe", "unobserve"] | |
Object.getOwnPropertyDescriptor(Array, 'prototype') //=> | |
//Prototype | |
//当一个函数对象被创建时,这个函数对象就具有一个prototype成员, | |
//这个成员是一个对象,这个对象包含了一个构造子成员,这个构造子成员会指向这个函数对象 | |
function Base(){ | |
this.id = 'foobar'; | |
} | |
var obj = new Base() //是不是很熟悉? 图示见文档 | |
//new 操作符干了什么? | |
//1、var obj = {}; | |
//2、obj.__proto__ = Base.prototype; | |
//3、Base.call(obj); | |
obj.print() //=> obj.print is not a function | |
Base.prototype.print = function() { | |
return this.id; | |
} | |
obj.print() //=> 'foobar' | |
//重写 | |
obj.print = function() {return 'balabala'} | |
obj.print() //=> 'balabala' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment