为什么使用Javascript对象而不是原型 [英] Why using Javascript object and not prototype

查看:54
本文介绍了为什么使用Javascript对象而不是原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哟,

我的问题是关于javascript对象。

My question is about javascript object.

我读过backbone.js代码我看到模型和对象正在使用javascript对象来定义一个对象。

I've read backbone.js code and i see model and object are using javascript object to define an object.

就像那样

Backbone.Model.extend({
    initialize: function() { ... },
    author: function() { ... },
    coordinates: function() { ... },
    allowedToEdit: function(account) {
        return true;
    }
});

为什么不使用原型?
因为它是每个类重新定义的方法吗?
因为创建的每个对象占用的空间都比backboneJS多?

Why not using prototype ? Because it's redefined method for every class ? Because each object created take more space than backboneJS ?

如果有人能够解释它何时以及为什么使用原型感兴趣?

If someone can explain it to me when and why it's interesting to use prototype ?

推荐答案

用于在Backbone中创建对象的扩展方法 USE 原型,你只是看不到它。 br>
至于另一个问题,我想你问的第一个问题的方式是对的:)。此外,从我看到的一些基准测试,如果您实例化许多对象,使用原型会更快。也就是说,如果你使用单例,你可能想要使用静态属性(extend(protoProp,staticProp))。

The extend method you use to create object in Backbone USE the prototype, you just don't see it.
As for the other question, I guess you got it right the way you asked your first questions :). Also, from some benchmarks I saw, using prototype is faster if you instantiate many objects. That said, if you use singleton, you may want to use static properties (extend(protoProp, staticProp)).

相关的Backbone的代码(扩展函数定义) :

var Surrogate = function(){ this.constructor = child; };
Surrogate.prototype = parent.prototype;
child.prototype = new Surrogate;

// Add prototype properties (instance properties) to the subclass,
// if supplied.
if (protoProps) _.extend(child.prototype, protoProps);

这篇关于为什么使用Javascript对象而不是原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆