NOTE: javascript prototype chain
2009-02-11 09:31
295 查看
=== copy from mastering dojo ===
What Is a Prototype?
Every object in JavaScript contains a reference to another
object termed its prototype. Since the prototype is another
object itself, it also contains a reference to its prototype. This
forms a chain of objects. The chain terminates with the prototype
for the built-in Object type.
When a property of an object is read, JavaScript looks for the
property in the object. If not found, JavaScript then looks in the
prototype object, the prototype of the prototype, and so on,
up the prototype chain until the property is found or the chain
is exhausted. Since a method is just a property that happens to
be a function, this is how method dispatching occurs, and this
system is called prototypal inheritance.
=== test ===
What Is a Prototype?
Every object in JavaScript contains a reference to another
object termed its prototype. Since the prototype is another
object itself, it also contains a reference to its prototype. This
forms a chain of objects. The chain terminates with the prototype
for the built-in Object type.
When a property of an object is read, JavaScript looks for the
property in the object. If not found, JavaScript then looks in the
prototype object, the prototype of the prototype, and so on,
up the prototype chain until the property is found or the chain
is exhausted. Since a method is just a property that happens to
be a function, this is how method dispatching occurs, and this
system is called prototypal inheritance.
=== test ===
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>test prototype</title> <body> <div id="debug"></div> <mce:script type="text/javascript"><!-- var obj = {a:-1, b: 1}; var TMP = function(){}; TMP.prototype = obj; var tmp = new TMP(); document.getElementById('debug').innerHTML += ("obj.a: " + obj.a) + '<br />'; document.getElementById('debug').innerHTML += ("tmp.a inherit from obj: " + tmp.a) + '<br />'; document.getElementById('debug').innerHTML += '<br />'; obj.a = 0; document.getElementById('debug').innerHTML += ("obj.a = " + obj.a) + '<br />'; document.getElementById('debug').innerHTML += ("tmp.a: " + tmp.a) + '<br />'; document.getElementById('debug').innerHTML += '<br />'; tmp.a = 1; document.getElementById('debug').innerHTML += ("tmp.a = " + tmp.a) + '<br />'; document.getElementById('debug').innerHTML += ("obj.a not changed: " + obj.a) + '<br />'; document.getElementById('debug').innerHTML += ("tmp.a: " + tmp.a) + '<br />'; document.getElementById('debug').innerHTML += '<br />'; obj.a = -100; document.getElementById('debug').innerHTML += ("obj.a = " + obj.a) + '<br />'; document.getElementById('debug').innerHTML += ("tmp.a: " + tmp.a) + '<br />'; document.getElementById('debug').innerHTML += ("TMP.prototype.a: " + TMP.prototype.a) + '<br />'; // --></mce:script> </body> </html>
相关文章推荐
- javascript prototype chain
- Javascript Prototype And Prototype Chain
- javascript prototype [note]
- [Javascript] Link to Other Objects through the JavaScript Prototype Chain
- Prototype chain in JavaScript
- javascript中构造函数、原型、原型链(prototype)
- JavaScript prototype 属性详解
- 浅谈javascript的Array.prototype.slice.call
- javascript中prototype属性的应用
- inheritPrototypeChain.js
- JavaScript 学习note (undefined null NaN)
- JavaScript-Prototype-new-继承or委托?
- javascript extends prototype to delay call a function
- 【笔记】javascript原型属性prototype
- JavaScript Object.prototype.constructor属性详解
- 封装——JavaScript的原型(prototype)
- javascript对象的property和prototype是这样一种关系
- 深入理解javascript原型和闭包(3)——prototype原型
- 跟我学习javascript的prototype原型和原型链
- 跟我学习javascript的prototype原型和原型链