Javascript objects contain key/value pairs. Js objects contains numbers, array, object, boolean, null, undefined functions under one single roof. This process of containing different properties and function under one single roof, or variable is called encapsulation.
eg. of Js object.
var obj={
'name' :'ashwini',
'age' :'25'
};
note: js objects are present within curly braces{}, key value pair are seperated by colon: and multiple key value pair are separated by comma,.
Different ways of object Creation.
Object literal way
you can create only one object. incase you want to create another, copy the entire structure again.
eg.
var a={
'name': 'fruit',
'color':'red'
};
var b={
'name':'mango',
'color':'yellow
}Factory functions
eg.
function fruit(name) {
return {
'color':'red',
'name':name
}
var apple=fruit('apple');Constructor function
function Fruit(name){
this.name=name,
this.color='red'
}
var apple = new fruit('apple');
'prototypal inheritance' is a concept in which constructor function creates object in more memory efficient way than factory function.
Top comments (0)