How do you create a method for a custom object in JavaScript?

前端 未结 7 1230
野的像风
野的像风 2020-11-28 22:21

Is it like...

var obj = new Object();

obj.function1 = function(){
    //code
}

or something like that?

相关标签:
7条回答
  • 2020-11-28 23:20

    You can also do the following instead of using the "Object.create()" method.

    Function call:

    com.blah.MyChildObj.prototype = createObject(com.blah.MyParentObj.prototype, 
        com.blah.MyChildObj);
    

    Function definition:

    function createObject(theProto, theConst) {
        function x() {};
        x.prototype = theProto;
        x.prototype.constructor = theConst;
        return new x();
    }
    
    0 讨论(0)
提交回复
热议问题