<body>
<div>
<p>Структурирование пространства</p>
<p>инкапсуляция (скрытие внутри лишней информации, для простоты,уменьшения связей)</p>
<p>полиморфизм (если есть похожие объекты,то их можно одинакого их обработать) - признакам объекта, свойства и методы одинаковые</p>
<p>наследование (выстраиваем в иерархию):</p>
<p>class-(спек, как надо сделать - вдругих языках) создаем прототип</p>
<p>function Dom(){ this.s=0 //this - новый объект,который будем делать }
</p>
<p>prototype - складывают методы, статические свойства</p>
<p>Dom.prototype={method:function(){this. ....}}</p>
</body>
JavaScript
// INGRIDIENTS с большой буквой, т.к. константа
var INGRIDIENTS = {
'cheese':{
weight: 50,
calories:50
},
'souce':{
weigt: 10,
calories: 20
},
'mozzarela': {
weight: 25,
calories: 25
}
}
function PizzaContructor(recipe) {
// правила создания объекта - его инициализация
// this который доступен в этой функции - это новый объект, который будет создан если функцию вызвать с new
this.base = options.base
this.ingridients = recipe.ingridients
this.weight = options.base.weight // weight - это свойство объекта
this.calories = options.base.calories
}
var pizza = new PizzaContructor({
base: {
weight: 100
}
})
console.log(pizza.weight) // 100
PizzaContructor.prototype = { // or Object.create
getWeight: function() { // 'эта функция называется метод объекта
// this такой же как в функции-конструкторе
// мы можем из него брать любые свойства
console.log(this, this.weight)
return this.weight
},
getCalories: function() {
return this.calories
}
}
var pizza = new PizzaContructor({
base: {
calories: 50,
weight: 100
}
})
//create function
var recipeMargarita = {
base: {
weight: 100,
calories: 30
},
ingridients: [
INGRIDIENTS.cheese,
INGRIDIENTS.souce,
INGRIDIENTS.mozzarela
]
}
console.log(pizza.getCalories())
setTimeout(pizza.getWeight, 50)
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.