create function in javascript with custom prototype

前端 未结 3 1205
名媛妹妹
名媛妹妹 2021-01-19 01:26

So I created a function like this,

var functionName = function(arg1) { //code logic here; }

At the same time, I need this function to work

3条回答
  •  温柔的废话
    2021-01-19 01:58

    You could set the prototype of the function.

    function functionName(arg1) { 
        //code logic here;
    }
    
    var myObj = { x: 3, y: 4 };
    functionName.prototype = myObj;
    
    var obj = new functionName();
    console.log(obj.x, obj.y); // will output 3, 4
    

提交回复
热议问题