Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/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 具有自定义“类”的函数_Javascript - Fatal编程技术网

Javascript 具有自定义“类”的函数

Javascript 具有自定义“类”的函数,javascript,Javascript,基本上,我想要一个有instanceof Something===true的函数 比如我的特殊函数,比如: 如果MySpecialKindof函数的函数实例 我试着在不工作的情况下设置函数的原型和构造函数。也许我不理解你的问题,但是你想实现这种目标吗 // Static class method. Interface.ensureImplements = function(object) { if(arguments.length < 2) { throw new

基本上,我想要一个有instanceof Something===true的函数

比如我的特殊函数,比如:

如果MySpecialKindof函数的函数实例
我试着在不工作的情况下设置函数的原型和构造函数。

也许我不理解你的问题,但是你想实现这种目标吗

// Static class method.
Interface.ensureImplements = function(object) {
    if(arguments.length < 2) {
        throw new Error("Function Interface.ensureImplements called with " +
        arguments.length + " arguments, but expected at least 2.");
    }
    for(var i = 1, len = arguments.length; i < len; i++) {
        var interface = arguments[i];
        if(interface.constructor !== Interface) {
            throw new Error("Function Interface.ensureImplements expects arguments"
            + " two and above to be instances of Interface.");
        }
        for(var j = 0, methodsLen = interface.methods.length; j < methodsLen; j++) {
            var method = interface.methods[j];
            if(!object[method] || typeof object[method] !== 'function') {
                throw new Error("Function Interface.ensureImplements: object "
                + "does not implement the " + interface.name
                + " interface. Method " + method + " was not found.");
            }
        }
    }
};
要扩展类,可以执行以下操作:

function Item () {

};

function Picture () {
    Item.call(this); // Call to superClass constructor with subclass instance
};


Picture.prototype = new Item();
Picture.prototype.constructor = Picture;

var picture = new Picture();
if(picture instanceof Item){ 
    alert("True");
} else {
    alert(false);
}

我想要一个函数,它是某物的实例。换言之,我更新了我的答案。在本例中,Picture是Item的子类。函数扩展可以在你想要的地方。我需要类是一个函数。这就是我所说的需要将函数子类化的意思。换句话说,一些var myVar,可以称为:myVar,它也是函数的某个子类的实例;myFunc=newmyfunction=>console.loghello;myFunc//这记录了hello。myFunc instanceof函数返回true。myFunc instancesof MyFunciton//也返回trueSubclassing natives是有问题的。看看外面的其他人。使用ECMA6可能是可行的,但我还没有研究过。
function Item () {

};

function Picture () {
    Item.call(this); // Call to superClass constructor with subclass instance
};


Picture.prototype = new Item();
Picture.prototype.constructor = Picture;

var picture = new Picture();
if(picture instanceof Item){ 
    alert("True");
} else {
    alert(false);
}