JS-Objects_creations
by manoj_antony32
JavaScript
/*--Type1--*/
var x = { name: 'mano', 123: 'test' }
x.name = 'antony';
x.age = 32;
//console.log(x)
/*--Type2--*/
var y = new Object();
name = 'bharathi';
y.name = 'divya';
const _this = this;
y.print = () => { console.log(_this.name) }
//console.log(y)
y.print()
/*--Type3--*/
var z = function() {
this.name = 'mano';
this.age = 32;
}
z.prototype.print = function() {
console.log(this.name)
}
var res = new z();
res.name = 'antony';
var res1 = new z();
console.log(res)
res.print()
res1.print()
// console.log(res1)
/*--Type4--*/
//var m = Object.create(Object.prototype);
var m = Object.create(null); //way of creating pure Object
console.log(m)
var k = { name: 'rajkumar' };
var j = Object.create(k, {age: {value: 27, enumerable: false}})
console.clear();
console.log(j)
//console.log(k.name)
var k = { name: 'rajkumar' };
Object.defineProperty(k, 'qualification', { value: 'mca', enumerable: true, writable: true})
k.qualification = 'BCA';
var j = Object.create(k, {age:
{
//value: 27, enumerable: true,
enumerable: true,
set: function(value) { this.name = value },
get: function() { return this.name }
}
})
//console.clear();
console.log(k)
console.log(j.age = 28)
console.clear();
var x = { name: 'antony', age:27};
x.name = 'dhanush';
//var y = { ...x };
var y = x; // reference pointed, this is mutation
y.name = 'manoj';
console.log(x);
console.log(y)