简体   繁体   English

什么是 es5 中的 ES6 方法(类)的等价物?

[英]Whats the equivalent of ES6 methods(class) in es5?

How would we polyfill es6 class methods into ES5?我们如何将 es6 类方法填充到 ES5 中?

I am reading a book and it says the following:我正在读一本书,上面写着:

class Ninja {
    constructor(name) {
        this.name = name;
    }

    swingSword() {
        return true;
    }
}

is the same as是相同的

function Ninja(name) {
    this.name = name;
} 

Ninja.prototype.swingSword = function() {
    return true;
};

I am just asking why are we adding the swingSword on the prototype and not inside the constructor function?我只是问为什么我们在原型上而不是在构造函数中添加 swingSword?

Because the function should be on the object and not on the prototype chain.因为函数应该在对象上,而不是在原型链上。

Am i right or wrong?我是对还是错?

It should be on the prototype, methods are not per-instance data.它应该在原型上,方法不是每个实例的数据。 Can't think of any language that implements it that way, the whole idea of classes is to have a whole class of objects that have the same set of methods.想不出有什么语言能以这种方式实现它,类的整个想法是拥有一整类具有相同方法集的对象。

If it was put it inside the constructor function, it would be a unique function per instance made with the constructor.如果将它放在构造函数中,它将是使用构造函数创建的每个实例的唯一函数。 eg, 1000 objects == 1000 functions, per "method".例如,每个“方法”有 1000 个对象 == 1000 个函数。

Adding the function to just the object would only work for a Ninja .仅将函数添加到对象仅适用于Ninja To create a class that extends Ninja , for example Kunoichi , you would normally copy the Ninja prototype.要创建一个扩展Ninja的类,例如Kunoichi ,您通常会复制Ninja原型。 Unfortunately, because swingSword is not in the prototype, your Kunoichi cannot swing swords.不幸的是,因为swingSword不在原型中,所以你的Kunoichi无法挥动剑。

You must add the function in prototype to allow the class to be extended.您必须在原型中添加该函数以允许扩展该类。

If we add a method to the prototype, only one instance of that method exists in memory, and it's shared between all objects created from the constructor.如果我们向原型添加一个方法,则该方法的一个实例仅存在于内存中,并且它在从构造函数创建的所有对象之间共享。

If we add the swingSword method directly to the Ninja constructor function, then every object would have its own copy of that method, taking up more memory.如果我们将 swingSword 方法直接添加到 Ninja 构造函数中,那么每个对象都会有自己的该方法的副本,从而占用更多内存。


var $class = function ($superclass, config) {
    // All classes have a superclass with the root 
    // of this $class hierarchy being Object.
    var self = function (config) {
        // Object.assign or $.extend or ...
        config && Object.assign(this, config);
    };
    self.prototype = new $superclass(config);
    return self;
};

var A = $class(Object, {
    sayWhat: "Hello, I'm an A",
    say: function () {
        console.log(this.sayWhat);
    }
});

var B = $class(A, {
    sayWhat: "Hello, I'm a B"
});

var C = $class(B, {
    say: function () {
        console.log("C SAYS: " + this.sayWhat);
    },
    superSay: function () {
        // how to call a superclass method
        B.prototype.say.call(this);
    }
});

var a = new A();
a.say();  // Hello, I'm an A

var b = new B();
b.say();  // Hello, I'm a B

var c = new C();
c.say();  // C SAYS: Hello, I'm a B

// create a "one-off" object
var d = new C({
    sayWhat: "I'm special!",
    say: function () {
        console.log("hey!");
    }
});
d.say();  // hey!
d.superSay();  // I'm special!
C.prototype.say.call(d);  // C SAYS: I'm special!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM