Objects

by LyndseyB

JavaScript

function Obj(name) {
    this.name = name;
    this.colour = 'red';
};

Obj.prototype.age = '28';

var myObj = new Obj('Lyndsey');

var stringObj = '{ "name": "Lyndsey", "age": "28" }';

//console.log(myObj.name); // Lyndsey
//console.log(myObj.age); // 28

//console.log(myObj.hasOwnProperty('name')); // true
//console.log('age' in myObj); // true
//console.log(myObj.hasOwnProperty('age')); // false - inherited

//delete myObj.age; // can't delete because its an inherited property
//delete myObj.name; // displays only age
//for(x in myObj.__proto__) {
//for(x in myObj) {
    //if(myObj.hasOwnProperty(x)) {
//    console.log(x);
   // }
//}

//console.log(JSON.stringify(myObj)); non formatted string
//console.log(JSON.stringify(myObj, null, 4)); format object string

//console.log(JSON.parse(stringObj)); // deserialize (convert string to object)

//console.log(Obj.prototype.constructor); // show constructor object
//console.log(Object.getPrototypeOf(myObj));

//console.log(typeof Obj); // function type
//console.log(Obj.length); // number of available arguments in Obj, length is inherited from Function.prototype. When we defined Obj, it automatically created a secret __proto__ property that set it's value to Function.prototype