Статьи

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

Читать:
Веб-аутентификация: файлы cookie или токены

Похожие записи

Из многих маршрутов вы можете продолжить изучение науки о данных и машинного обучения (аспирантура, MOOC, учебные лагеря, самообучение и т. Д.), Которые…

admin

Как мы настроили Mysql для хранения смайлов в YourQuote?

admin

PHP 7.x — P41: стрелочные функции

admin

NSPredicate + Objective-C

admin

Я не могу с тобой больше согласиться. И все же это часто вызывает споры!

admin

Программы-вымогатели :почему это так просто и имеет такой экономический смысл

admin