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

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

Is it like...

var obj = new Object();

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

or something like that?

7条回答
  •  -上瘾入骨i
    2020-11-28 23:06

    Don't worry bro, here the code is:

      var myObj=function(){
          var value=null
    
         this.setValue=function(strValue){
    
         this.value=strValue;
         };
    
         this.getValue=function(){
         return this.value;
         };    
    };
    

    You can call this object like this:

        var obj= new myObj();
        obj.setValue("Hi!");
        alert(obj.getValue());
    

提交回复
热议问题