You are on page 1of 5

Object literal Approach

var myCat = {
name: "pussy",
color:"white",
speak: function(){
console.log("Meeeow")
}
}

myCat // object
myCat.speak() // "Meeeow"

-----------
"use strict";
var cat = {
name: "pussy",
color: "white"
}
Object.defineProperty(cat,"name",{writable:false})
cat.name = "cool"

// throws an error

but if we run it in normal mode. it will not throw an error.


So always run it in strict mode

-------------

"use strict";
var cat = {
name: {fname:"sam",lname:"sahoo"},
color: "white"
}
Object.defineProperty(cat,"name",{writable:false})
cat.name = "cool" // throws and error

but if we do something like this


cat.name.fname = "cool"
cat.name // {fname:"cool",lname:"sahoo"}

----------------
if we do something
Object.freeze(cat.name) // we can't change cat.name.fname value also

----------------
if we make configurable to "false"

1. you cant change "configurable" attribute


2. you cant change "enumerable" attribute
3. you cant delete a property

-------------
getters and setters

"use strict"
var cat = {
name: {fname:"sam",lname:"sahoo"},
color: "white"
}
Object.defineProperty(cat,"fullname",{
get: function(){
return this.name.fname + " " + this.name.lname; // "sam sahoo"
},
set: function(value){
var nameParts = value.split(" ");
this.name.fname = nameParts[0];
this.name.lname = nameParts[1];
}
})

cat.fullname // sam sahoo


cat.fullname = "pari priya"
cat.fullname // pari priya
cat.fullname.fname // pari
cat.fullname.lname // priya

------------------

// how ever we can create our own property for this

var arr = ["one","two","three"];


Object.defineProperty(arr,"last",{
get: function(){
return this[this.length-1]
}
})
arr.last // three

// here there is a problem


arr2 = ["four","five","six"];
arr2.last // undefined

// so if we add "last" property to Array.prototype then it will work

var arr = ["one","two","three"];


arr2 = ["four","five","six"];
Object.defineProperty(Array.prototype,"last",{
get: function(){
return this[this.length-1]
}
})

arr.last
arr2.last

**************************
So what is a prototype?

A prototype is a function that exist in every function in javascript

var myFunc = function(){}


console.log(myFunc.prototype) // {} i.e an empty object

// but prootype is not exist in object

var cat = {name:"pussy"}


console.log(cat.prototype) // undefined
console.log(cat.__proto__) // {}

***********

function Cat(name,color){
this.name = name;
this.color = color;
}
Cat.prototype.age = 12;

myCat1 = new Cat("pussy","white");


myCat2 = new Cat("pussy2","white2");

myCat1.age = 30;
myCat1.age // 30
myCat1.__proto__.age // 12

myCat2.age // 12
myCat2.hasOwnProperty("age") // false
myCat1.hasOwnProperty("age") // true

?? here age is coming from its prototype

////

function Animal(){

}
Animal.prototype.speak = function(){
console.log("meaowww")
}
function Cat(name,color){
this.name = name;
this.color = color;
}
Cat.prototype = Object.create(Animal.prototype)
myCat = new Cat("pussy","white");
myCat.speak() // meaowww

Why we did this instad of new Animal()?????


Cat.prototype = Object.create(Animal.prototype)

Because Object.create will not go and call the animal function.


It only setup the prototype chain.
If we call the new Animal(), it will go and execute the animal function

*********************

function Animal(voice){
this.voice = voice || "meaowww"
}
Animal.prototype.speak = function(){
console.log(this.voice)
}
function Cat(name,color){
Animal.call(this,"gerrrrr")
this.name = name;
this.color = color;
}
Cat.prototype = Object.create(Animal.prototype)
myCat = new Cat("pussy","white");
myCat.speak() // gerrrrr
console.log(myCat.constructor) // animal

But we want our constructor to be Cat


so
Cat.prototype.constructor = Cat

console.log(myCat.__proto__) // Cat{}
console.log(myCat.__proto__.__proto__) // Animal{}

*******************
Class Pattern

"use strict";
class Cat{
constructor(name,color){
this.name = name;
this.color = color;
}
speak(){
console.log("Meeeow")
}
}
var myCat = new Cat("pussy","white");

Class Pattern - 2

"use strict";
class Animal{
constructor(voice){
this.voice = voice || "meaowww"
}
speak(){
console.log(this.voice)
}
}

class Cat extends Animal{


constructor(name,color){
super("gerrrrr")
this.name = name;
this.color = color;
}
}

var myCat = new Cat("pussy","white");


console.log(myCat)
// Animal {voice: "gerrrrr", name: "pussy", color: "white"}

-----------------------------------------------------------------------------------
-----------------------

var obj1 = {
name : "dasd"
}

obj1.foo() // typeerror: foo is not a function


obj1.toString() // [object object]
typeof Object.prototype // Object
typeof Object.prototype.toString // function
obj1.prototype // undefined
typeof obj1.__proto__ // Object
obj1.__proto__ === Object // false
obj1.__proto__ === Object.prototype // true

********
Object.create()

obj2 = Object.create(obj1);

obj2.__proto__ === Object.prototype // false


obj2.__proto__.__proto__ === Object.prototype // true

You might also like