[Javascript]Object_constructor

Suyeon·2020년 8월 24일
0

Javascript

목록 보기
6/31

Constructor

  • When we need to create many similar objects.
  • They are named with capital letter first.
  • They should be executed only with "new" operator.
function User(name) {
  // this = {};  (implicitly)
  this.name = name;

  this.sayHi = function() {
    alert( "My name is: " + this.name );
  };
  // return this;  (implicitly)
}

let john = new User("John");

john.sayHi(); // My name is: John

✔️ return with an object returns that object, in all other cases this is returned.

profile
Hello World.

0개의 댓글