Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 原型继承上的JS getter/setter_Javascript_Oop_Inheritance_Javascript Inheritance - Fatal编程技术网

Javascript 原型继承上的JS getter/setter

Javascript 原型继承上的JS getter/setter,javascript,oop,inheritance,javascript-inheritance,Javascript,Oop,Inheritance,Javascript Inheritance,最近我在es6课程中编写了一个小库。现在我决定将其重写为es5代码。因为库经常使用类继承(类a扩展了类B),所以我必须编写一小段代码来模拟es6类继承: combine.js 有没有一种方法可以继承函数原型中定义的getter/setter呢?最后,感谢@Bergi的链接,我得到了mixin函数的工作原型,如下所示: module.exports = function () { var _arguments = arguments; var Class = function

最近我在es6课程中编写了一个小库。现在我决定将其重写为es5代码。因为库经常使用类继承(类a扩展了类B),所以我必须编写一小段代码来模拟es6类继承:

combine.js


有没有一种方法可以继承函数原型中定义的getter/setter呢?

最后,感谢@Bergi的链接,我得到了mixin函数的工作原型,如下所示:

module.exports = function () {

    var _arguments = arguments;

    var Class = function () {
        for (var i = 0; i < _arguments.length; i++) {
            if (typeof _arguments[i] === 'function') {
                _arguments[i].call(this);
            }
        }
    }

    var prototype = { }
    for (var x = 0; x < _arguments.length; x++) {
        if (typeof _arguments[x] === 'function') {
            var properties = Object.getOwnPropertyNames(_arguments[x].prototype);
            for (let y in properties) {
                if (properties[y] != 'constructor') {
                    Object.defineProperty(
                        prototype,
                        properties[y],
                        Object.getOwnPropertyDescriptor(_arguments[x].prototype, properties[y])
                    );
                }
            }
        }
    }
    Class.prototype = prototype;

    return Class;

}
module.exports=函数(){
变量_参数=参数;
变量类=函数(){
对于(var i=0;i<\u arguments.length;i++){
if(参数类型[i]=“函数”){
_参数[i]。调用(此);
}
}
}
var原型={}
对于(var x=0;x<\u arguments.length;x++){
if(参数类型[x]=“函数”){
var properties=Object.getOwnPropertyNames(_参数[x].prototype);
for(让y进入属性){
if(属性[y]!=“构造函数”){
Object.defineProperty(
原型,
属性[y],
Object.getOwnPropertyDescriptor(_参数[x]。原型,属性[y])
);
}
}
}
}
Class.prototype=原型;
返回舱;
}

最后,多亏@Bergi的链接,我得到了mixin函数的工作原型,它看起来像这样:

module.exports = function () {

    var _arguments = arguments;

    var Class = function () {
        for (var i = 0; i < _arguments.length; i++) {
            if (typeof _arguments[i] === 'function') {
                _arguments[i].call(this);
            }
        }
    }

    var prototype = { }
    for (var x = 0; x < _arguments.length; x++) {
        if (typeof _arguments[x] === 'function') {
            var properties = Object.getOwnPropertyNames(_arguments[x].prototype);
            for (let y in properties) {
                if (properties[y] != 'constructor') {
                    Object.defineProperty(
                        prototype,
                        properties[y],
                        Object.getOwnPropertyDescriptor(_arguments[x].prototype, properties[y])
                    );
                }
            }
        }
    }
    Class.prototype = prototype;

    return Class;

}
module.exports=函数(){
变量_参数=参数;
变量类=函数(){
对于(var i=0;i<\u arguments.length;i++){
if(参数类型[i]=“函数”){
_参数[i]。调用(此);
}
}
}
var原型={}
对于(var x=0;x<\u arguments.length;x++){
if(参数类型[x]=“函数”){
var properties=Object.getOwnPropertyNames(_参数[x].prototype);
for(让y进入属性){
if(属性[y]!=“构造函数”){
Object.defineProperty(
原型,
属性[y],
Object.getOwnPropertyDescriptor(_参数[x]。原型,属性[y])
);
}
}
}
}
Class.prototype=原型;
返回舱;
}

“现在我决定将其重写为es5代码。”-为什么?让transpiler为您完成这项工作。“
B.prototype.isLoading(){返回这个。_loading;}
”是一个语法错误。如果没有,把你的internet explorer扔掉。是的,谢谢你,只是示例中的一个错误,我实际上没有使用这个代码,
combine
东西并没有真正实现继承,它更像是应用mixin。你为什么不直接用呢?@Bergi是对的。让巴贝尔为你做这项工作(让他自然死亡)。看看,或者“现在我决定把它重写成es5代码。”-为什么?让transpiler为您完成这项工作。“
B.prototype.isLoading(){返回这个。_loading;}
”是一个语法错误。如果没有,把你的internet explorer扔掉。是的,谢谢你,只是示例中的一个错误,我实际上没有使用这个代码,
combine
东西并没有真正实现继承,它更像是应用mixin。你为什么不直接用呢?@Bergi是对的。让巴贝尔为你做这项工作(让他自然死亡)。看,或者
module.exports = function () {

    var _arguments = arguments;

    var Class = function () {
        for (var i = 0; i < _arguments.length; i++) {
            if (typeof _arguments[i] === 'function') {
                _arguments[i].call(this);
            }
        }
    }

    var prototype = { }
    for (var x = 0; x < _arguments.length; x++) {
        if (typeof _arguments[x] === 'function') {
            var properties = Object.getOwnPropertyNames(_arguments[x].prototype);
            for (let y in properties) {
                if (properties[y] != 'constructor') {
                    Object.defineProperty(
                        prototype,
                        properties[y],
                        Object.getOwnPropertyDescriptor(_arguments[x].prototype, properties[y])
                    );
                }
            }
        }
    }
    Class.prototype = prototype;

    return Class;

}