Last active
January 8, 2019 09:20
-
-
Save vivektikar25/c3b6d88ccd81b6563df4ab7d19b86dab to your computer and use it in GitHub Desktop.
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
// Wriatble Demo | |
var obj = { }; | |
Object.defineProperty(obj, "property1", { | |
value: "foo", | |
writable:true, | |
enumerable:true, | |
configurable:true | |
}); | |
Object.defineProperty(obj, "property1", { | |
writable:false | |
}); | |
// Enumerable Demo | |
for (key in obj){ | |
console.log(key); | |
} | |
Object.defineProperty(obj, "property1", { | |
enumerable:false | |
}); | |
for (key in obj){ | |
console.log(key); | |
} | |
// Configurable Demo | |
Object.defineProperty(obj, "property1", { | |
writable:true | |
}); | |
Object.defineProperty(obj, "property1", { | |
configurable:false | |
}); | |
Object.defineProperty(obj, "property1", { | |
writable:false | |
}); | |
var person = { | |
firstName: "John", | |
lastName: "Stone" | |
} | |
Object.defineProperty(person, 'fullName', { | |
get: function () { | |
return this.firstName + ' ' + this.lastName; | |
}, | |
set: function (value) { | |
var splitString = value.trim().split(' '); | |
this.firstName = splitString[0]; | |
this.lastName = splitString[1]; | |
} | |
}); | |
console.log(person.fullName); // "John Stone" | |
person.fullName = "David Schwimmer"; | |
console.log(person); // {firstName: "David", lastName: "Schwimmer"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment