How to “properly” create a custom object in JavaScript?

后端 未结 15 1970
被撕碎了的回忆
被撕碎了的回忆 2020-11-21 08:07

I wonder about what the best way is to create an JavaScript object that has properties and methods.

I have seen examples where the person used var self = this<

15条回答
  •  执念已碎
    2020-11-21 08:46

    var Person = function (lastname, age, job){
    this.name = name;
    this.age = age;
    this.job = job;
    this.changeName = function(name){
    this.lastname = name;
    }
    }
    var myWorker = new Person('Adeola', 23, 'Web Developer');
    myWorker.changeName('Timmy');
    
    console.log("New Worker" + myWorker.lastname);
    

提交回复
热议问题