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

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

问题描述

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

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.

此外,使用函数字面量定义类"是否比仅使用函数定义有任何好处:

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

var Class = function () {};

对比

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

注意如何更改应用于两个实例的方法?这是因为 ins1ins2 共享相同的 calc() 函数.为了使用在构造期间创建的公共方法来执行此操作,您必须将新方法分配给已创建的每个实例,这是一项笨拙的任务.这是因为 ins1ins2 将拥有自己的、单独创建的 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天全站免登陆