Adding members to an existing object

后端 未结 5 1409
鱼传尺愫
鱼传尺愫 2020-12-13 19:46

Suppose we have the following object:

var obj = {
    fn1: function() {
    }
}

how can I dynamically add another member to it, say

5条回答
  •  遥遥无期
    2020-12-13 20:18

    you can use prototype for that...

    obj.prototype.fn2 = function() {
     ....
    }
    

    or just simply

    obj.fn2 = function() {
     ....
    }
    

提交回复
热议问题