javascript

JavaScript/ Object and Properties

MyaZ 2020. 2. 13. 19:09

// ch25 Object and properties

- object literal: the way of define the object

var john = {
	firstName: "John",
	//key: value
	lastName: "Smith",
	birthYear: 1990,
	family: ["jane", "Mark", "Bob"],
	job: "teacher",
	isMarried: false
	};

console.log(john.firstName);//:'firstName' is a property of the 'Object john'.
console.log(john["lastName"]);
var x = "birthYear";
console.log(john[x]);


- mutation of the object

john.job = "designer"; 
john["isMarried"] = true; 
console.log(john) //print 
/*  
 firstName: "John" 
 lastName: "Smith" 
 birthYear: 1990 
 family: (3) ["jane", "Mark", "Bob"] 
 job: "designer" 
 isMarried: true 
 */ 

 

- new object syntax

var jane = new Object(); 
jane.firstName = "Jane"; 
jane.birthYear = 1978; 
jane["lastName"] = "Smith"; 
console.log(jane);