使用原型的优点,直接在构造函数中定义方法? [英] Advantages of using prototype, vs defining methods straight in the constructor?

查看:137
本文介绍了使用原型的优点,直接在构造函数中定义方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道使用其中任何一个是否有任何优势,我应该走哪条路?

I am wondering if there are any advantages of using any of these over the other, and which way should I go?

构造方法:

var Class = function () {

    this.calc = function (a, b) {
        return a + b;
    };

};

原型方法:

var Class = function () {};

Class.prototype.calc = function (a, b) {
    return a + b;
};

我不喜欢这样,使用原型,方法定义与类分开,而我我不知道是否有任何具体原因我应该在第一种方法中使用它。

I don't like that, using the prototype, method definitions are separated from the class, and I'm not aware if there is any specific reason I should use this over just the first approach.

此外,使用函数文字定义是否有任何好处 class,仅仅是函数定义:

Also, is there any benefit of using a function literal to define a "class", over just function definition:

var Class = function () {};

vs

function Class () {};

谢谢!

推荐答案

通过原型链继承的方法可以针对所有实例进行普遍更改,例如:

Methods that inherit via the prototype chain can be changed universally for all instances, for example:

function Class () {}
Class.prototype.calc = function (a, b) {
    return a + b;
}

// Create 2 instances:
var ins1 = new Class(),
    ins2 = new Class();

// Test the calc method:
console.log(ins1.calc(1,1), ins2.calc(1,1));
// -> 2, 2

// Change the prototype method
Class.prototype.calc = function () {
    var args = Array.prototype.slice.apply(arguments),
        res = 0, c;

    while (c = args.shift())
        res += c;

    return res; 
}

// Test the calc method:
console.log(ins1.calc(1,1,1), ins2.calc(1,1,1));
// -> 3, 3

请注意如何更改应用于这两个实例的方法?这是因为 ins1 ins2 共享相同的 calc()功能。为了使用在构造期间创建的公共方法执行此操作,您必须将新方法分配给已创建的每个实例,这是一项尴尬的任务。这是因为 ins1 ins2 将拥有自己的,单独创建的 calc()函数。

Notice how changing the method applied to both instances? This is because ins1 and ins2 share the same calc() function. In order to do this with public methods created during construction, you'd have to assign the new method to each instance that has been created, which is an awkward task. This is because ins1 and ins2 would have their own, individually created calc() functions.

在构造函数中创建方法的另一个副作用是性能较差。每次构造函数运行时都必须创建每个方法。原型链上的方法创建一次,然后由每个实例继承。在硬币的另一面,公共方法可以访问私有变量,这对于继承的方法是不可能的。

Another side effect of creating methods inside the constructor is poorer performance. Each method has to be created every time the constructor function runs. Methods on the prototype chain are created once and then "inherited" by each instance. On the flip side of the coin, public methods have access to "private" variables, which isn't possible with inherited methods.

至于你的 function Class(){} vs var Class = function(){} 问题,前者是悬挂到当前的顶部执行前的范围。对于后者,变量声明被提升,但不是赋值。例如:

As for your function Class() {} vs var Class = function () {} question, the former is "hoisted" to the top of the current scope before execution. For the latter, the variable declaration is hoisted, but not the assignment. For example:

// Error, fn is called before the function is assigned!
fn();
var fn = function () { alert("test!"); } 

// Works as expected: the fn2 declaration is hoisted above the call
fn2();
function fn2() { alert("test!"); }

这篇关于使用原型的优点,直接在构造函数中定义方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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