JS-конструктор объектов

#новый объект()

var person = new object();
person.firstname = 'default';
person.lastname = 'default';
person.getFullName = function(){
  return firstname + ' ' + lastname;
}

#буквальное обозначение

var person = {
  firstname : 'default',
  lastname : 'default',
  getFullName: function(){
    return firstname + '' + lastname;
  }
}

#конструктор функций и прототипирование

function PersonFn(firstname,lastname){
  this.firstname = firstname;
  this.lastname = lastname;
  
  this.getFullName = function(){
    return this.firstname + ' ' + this.lastname;
  }
}
personFn.prototype.changeName = function(firstname, lastname){
 
 this.firstname = firstname;
  this.lastname = lastname;
 //return this.firstname + ' ' + this.lastname;
}

//eunsim kang
var person1 = new personFn('eunsim','kang');
console.log(person1.getFullName()); // eunsim kang
person1.changeName('blossom','kang')
console.log(person1.getFullName()) // blossom kang

#калькулятор

function Calculator() {
 //this.a, this.b;
 this.read = function() {
   this.a = Number(window.prompt("a?",0));
   this.b = Number(window.prompt("b?",0));
   return [this.a , this.b];
  }
  this.sum = function(){
   return this.a + this.b
  }
  this.mul = function(){
   return this.a * this.b
  }
  
}
var calc = new Calculator();
calc.read();
console.log(calc.sum());
console.log(calc.mul());

#ref

https://javascript.info/constructor-new#create-new-calculator

См. также:  Лучший способ объединить переменные в наборе данных панели в R?
Понравилась статья? Поделиться с друзьями:
IT Шеф
Добавить комментарий

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: