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

前端 未结 7 1232
野的像风
野的像风 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:03

    With es6 you can do it like this:

    var a = {
        func(){
            return 'The value';
        }
    }
        
    document.getElementById('out').innerHTML = a.func();
       

提交回复
热议问题