Answer the question
In order to leave comments, you need to log in
What is the difference between a prototype method (static method) and a constructor function method?
var Juice = function (price) {
this.price = price;
this.apple = function ( ) { // метод объекта
console.log("Hi I'm apple juice.");
};
};
Juice.prototype.banana = function ( ) { // метод прототипа Juice
console.log("Hi I'm banana juice");
};
Juice.orange = function ( ) { // метод функции-конструктора (некоторые называют статический метод)
console.log("Hi I'm orange juice.");
};
var littleJuice = new Juice(100);
var littleJuice = new Juice(100);
var littleJuice = new Juice(100);
littleJuice
, the price and methods of apple are laid in them, memory is allocated for all this. banana
, it was registered once in the Juice prototype, a memory space was allocated for it 1 time, and it "does not multiply" anymore. orange
? What is a constructor method, what is it for? Answer the question
In order to leave comments, you need to log in
Thus, you have made a static function orange (public static orange), which can only be called as Juice.orange() and from this function you do not have access to the instance, of course, in your language "does not multiply with objects".
function Juice(kind, price){
let kind = kind; // это будет приватным свойством
this.price = price; // публичное свойство
this.kind = () => kind; //это публичный метод
this.hello = function(){
console.log(`Thats ${kind} juice`)
}
}
Juice.prototype.world = function(){
//приватное свойство kind тут не доступно, потому обратимся к публичному методу
console.log(`Juice kind: ${this.kind()}`)
}
// шаблон "фабрика"
Juice.apple = ( price ) => new Juice('apple', price);
Juice.orange = ( price ) => new Juice('orange', price);
let littleAppleJuice = Juice.apple(100),
hugeBananaJuice = new Juice('banana', 500);
littleAppleJuice.hello();
littleBananaJuice.world();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question