Add new element to an existing object

后端 未结 6 1816
走了就别回头了
走了就别回头了 2020-12-13 12:41

I was looking for a way to add new elements to an an existing object like what push does with arrays

I have tried this and it didn\'t work :

var myF         


        
6条回答
  •  感动是毒
    2020-12-13 13:10

    Use this:

    myFunction.bookName = 'mybook';
    myFunction.bookdesc = 'new';
    

    Or, if you are using jQuery:

    $(myFunction).extend({
        bookName:'mybook',
        bookdesc: 'new'
    });
    

    The push method is wrong because it belongs to the Array.prototype object.

    To create a named object, try this:

    var myObj = function(){
        this.property = 'foo';
        this.bar = function(){
        }
    }
    myObj.prototype.objProp = true;
    var newObj = new myObj();
    

提交回复
热议问题