Backbone.js'constructor.prototype'和'__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.nameparent.__proto__.name看起来完全一样。为什么Backbone制作了两个有效且外观完全相同的字段?

constructor.prototype&中有什么区别__proto__吗?

罗希特416

constructor.prototype&中有什么区别__proto__吗?

__proto__是实际对象,用于解析查找链上的属性和方法。JavaScript具有实现通过原型完成的继承的能力。每一个建于物体可以使用扩展prototype其还用于创建一个新的__proto__

function Foo()
{
}

Foo.constructor.prototype == Foo.__proto__;  // returns true

但是这里发生了什么...

Foo.prototype == Foo.__proto__;  // returns false;

必须在这里阅读此主题!

这就是为什么您发出警报时得到相同结果的原因。该事物__proto__并不意味着要公开/扩展,并且从不建议这样做因此,您不必担心它们会产生相同的结果。Backbone可能扩展了其自定义函数/方法的原型,但是JavaScript为其创建__proto__了蓝图。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

JS:class.prototype.constructor和class.constructor之间有区别吗

__proto__与Constructor.prototype有何不同?

在javascript中将值设置为__proto__`和`prototype

为什么obj.constructor.prototype并不总是等于obj .__ proto__?

为什么obj.constructor.prototype并不总是等于obj .__ proto__?

Meteor,Ember.js和Backbone.js之间的主要区别是什么?

Backbone.js中的路由器和Ruby on Rails中的路由器之间的区别?

PROTOTYPE的ProxyMode和REQUEST范围之间的区别

Function和Function.prototype之间的区别

Object.prototype .__ proto__不为null

分类器.__proto__ = LogisticRegressionClassifier.prototype

__proto__和原型差异

1.constructor和(1).constructor之间的区别

class.prototype.property和object.property之间的区别

Object.toString和Object.prototype.toString之间的区别

isPrototypeOf 和 __proto__ 有不同的结果

打字稿和__proto__属性

JS中的类方法与className.prototype方法之间的区别

Backbone.Layout.extend和Backbone.view.extend之间的区别

构造函数属性:__proto __。constructor vs prototype.constructor

混淆“ Object .__ proto__ === Function.prototype”返回“ true”

Function.prototype .__ proto__是什么意思?

[] .__ proto__ === Array.prototype // === [Symbol(Symbol.unscopables):对象]?

Backbone.js和jQuery

Lodash和Underscore.js之间的区别

node.js和Tornado之间的区别

{{::somevalue}} 和 {{somevalue}} 之间的 Angular js 区别

react.js和Ajax之间的区别

。$ mount()和el之间的区别[Vue JS]