Object in Javascript are “name value pairs” and this name value pairs are called Object Properties
for Example :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var person = { | |
name : "Deepak", | |
place : "delhi" | |
}; |
person is Object which has two properties name and place
setting property
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
person.job = "Javascript Developer"; | |
person["job"] = "Javascript Developer"; |
getting property
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
person.name // "deepak" | |
person.place // "delhi" | |
person.job // "Javascript Developer" | |
person["name"] // "deepak" | |
person["place"] // "delhi" | |
person["job"] // "Javascript Developer" |
There are three way to create object in Javascript
- using an object literal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var person = { | |
name : "Deepak", | |
place : "delhi" | |
}; |
- using new keyword
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var person = new Object(); | |
person.name = "deepak"; | |
person.place = "delhi"; |
- inheriting Object.prototype
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var person = Object.create(Object.prototype); | |
person.name = "deepak"; | |
person.place = "delhi"; |
Everything in Javascript is Object
Everything in Javascript is Object except primitive values
primitive values are strings, numbers, true, false and undefined.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var str = "deepak"; | |
console.log(typeof str); // string | |
var num = 2; | |
console.log(typeof num); // number | |
var bool = true; | |
console.log(typeof bool); // boolean | |
var x; | |
console.log(typeof x); // undefined |
Dates are always objects
Arrays are always objects
Functions are always objects
Objects are objects
See the link of examples
Object in Javascript
Type of operator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var date = new Date(); | |
console.log(typeof date); // object | |
var date = ["deepak", "chetan", "narendra"]; | |
console.log(typeof date); // object | |
function myFunction(a, b) { | |
return a + b; | |
} | |
console.log(typeof myFunction); // function | |
var person = { | |
name : "deepak", | |
place : "delhi" | |
}; | |
console.log(typeof person); // object |
See the link of examples
Object in Javascript
Type of operator
No comments:
Post a Comment