Backbone.js 'constructor.prototype' 和 '__proto__' 之间的区别?
Difference between Backbone.js 'constructor.prototype' and '__proto__'?
在 Backbone 中实现了显示继承的代码。
GrandParent.js:
define([ "require", "jquery", "backbone"], function(require, $, Backbone) {
return Backbone.Model.extend({
//for method, variable override
name : "grandFa", //@ WILL BE Overrided
familyName : "Song",
age : 99,
sayThis : function() { //@ WILL BE Overrided
alert("GrandParent.this : "+this+" "+this.name);
}
});
});
Parent.js:
define([ "require", "jquery", "backbone", "grandParent"], function(require, $, Backbone, GrandParent) {
return GrandParent.extend({
//for testing complicated this usage
age : 50,
//@Overrided
name : "father",
sayThis : function() {
alert("Parent.this : "+this+" "+this.name);
}
});
});
Main.js:
require.config({
paths : {
jquery : "libs/jquery-2.1.0",
underscore : "libs/underscore-1.6.0",
backbone : "libs/backbone-1.1.0",
grandParent : "1-GrandParent",
parent : "2-Parent"
}
});
require(
["jquery","underscore", "backbone","grandParent","parent","child"], function($, _,Backbone, GrandParent, Parent, Child) {
$(document).ready(function() {
var grandParent = new GrandParent();
var parent = new Parent();
alert(parent.constructor.prototype.name); //=> parent
alert(parent.__proto__.name); //=> parent
});
});
在这段代码中,parent.constructor.prototype.name
和 parent.__proto__.name
看起来完全一样。为什么 Backbone 制作了 2 个看起来完全一样的字段?
constructor.prototype
和__proto__
有区别吗?
Is there any differences in constructor.prototype
& __proto__
?
__proto__
是实际对象,用于解析查找链上的属性和方法。 JavaScript 有能力实现通过原型完成的继承。每个内置对象都可以使用 prototype
进行扩展,进一步用于创建新的 __proto__
例如
function Foo()
{
}
Foo.constructor.prototype == Foo.__proto__; // returns true
但是这里发生了什么...
Foo.prototype == Foo.__proto__; // returns false;
Must read on this topic here!.
这就是为什么当您发出警报时得到相同结果的原因。问题是 __proto__
并不意味着 expose/extend,而是 never recommended。所以你不应该担心他们给出相同的结果。 Backbone 可能已经扩展了他们自定义 functions/methods 的原型,但正是 JavaScript 创建了 __proto__
作为他们的蓝图。
在 Backbone 中实现了显示继承的代码。
GrandParent.js:
define([ "require", "jquery", "backbone"], function(require, $, Backbone) {
return Backbone.Model.extend({
//for method, variable override
name : "grandFa", //@ WILL BE Overrided
familyName : "Song",
age : 99,
sayThis : function() { //@ WILL BE Overrided
alert("GrandParent.this : "+this+" "+this.name);
}
});
});
Parent.js:
define([ "require", "jquery", "backbone", "grandParent"], function(require, $, Backbone, GrandParent) {
return GrandParent.extend({
//for testing complicated this usage
age : 50,
//@Overrided
name : "father",
sayThis : function() {
alert("Parent.this : "+this+" "+this.name);
}
});
});
Main.js:
require.config({
paths : {
jquery : "libs/jquery-2.1.0",
underscore : "libs/underscore-1.6.0",
backbone : "libs/backbone-1.1.0",
grandParent : "1-GrandParent",
parent : "2-Parent"
}
});
require(
["jquery","underscore", "backbone","grandParent","parent","child"], function($, _,Backbone, GrandParent, Parent, Child) {
$(document).ready(function() {
var grandParent = new GrandParent();
var parent = new Parent();
alert(parent.constructor.prototype.name); //=> parent
alert(parent.__proto__.name); //=> parent
});
});
在这段代码中,parent.constructor.prototype.name
和 parent.__proto__.name
看起来完全一样。为什么 Backbone 制作了 2 个看起来完全一样的字段?
constructor.prototype
和__proto__
有区别吗?
Is there any differences in
constructor.prototype
&__proto__
?
__proto__
是实际对象,用于解析查找链上的属性和方法。 JavaScript 有能力实现通过原型完成的继承。每个内置对象都可以使用 prototype
进行扩展,进一步用于创建新的 __proto__
例如
function Foo()
{
}
Foo.constructor.prototype == Foo.__proto__; // returns true
但是这里发生了什么...
Foo.prototype == Foo.__proto__; // returns false;
Must read on this topic here!.
这就是为什么当您发出警报时得到相同结果的原因。问题是 __proto__
并不意味着 expose/extend,而是 never recommended。所以你不应该担心他们给出相同的结果。 Backbone 可能已经扩展了他们自定义 functions/methods 的原型,但正是 JavaScript 创建了 __proto__
作为他们的蓝图。