【发布时间】:2016-10-29 01:22:03
【问题描述】:
在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__有什么区别吗?
【问题讨论】:
-
其实这两个字段都不是Backbone做的。
标签: javascript backbone.js prototype-chain