JS Objects

by SriSri M

JavaScript

//Property names can be a string or a number, but if the property name is a number, it has to be accessed with the bracket notation. 
var obj = {name: "Sri", 26:"M"};
console.log(obj.name +" "+obj['26']);
//console.log(obj.26);
//console.log(obj['26']);

//Refrence vs Primitive
var company = "Carmatec"; //Primitive as the value stored directly.
var anotherCompany = company; //Reference data type as it stores the value of another variable as a reference.
company = "ColorCuboid";
console.log(company);
console.log(anotherCompany);

//saved-as-value vs saved-as-refrence
var myname = {name: "Sri Sri"}; //saved-as-refrence
var anotherName = myname; //saved-as-refrence
myname.name = "Chandru"; //saved-as-value
console.log(myname.name);
console.log(anotherName.name);

var mango = {
	color: "yellow",
	shape: "round",
	sweetness: 8,
	howSweetAmI: function () {
	console.log("Hmm Hmm Good");
	}
	}
  console.log(mango.howSweetAmI());
  
  var mangoFruit = {
	color: "yellow",
	sweetness: 8,
	fruitName: "Mango",
	nativeToLand: ["South America", "Central America"],
  
	showName: function () {
	console.log("This is " + this.fruitName);
	},
	nativeTo: function () {
	 this.nativeToLand.forEach(function (eachCountry)  {
	            console.log("Grown in:" + eachCountry);
	        });
	}
	}
  console.log(mangoFruit.nativeTo());
  console.log(mangoFruit.showName());