Created
May 12, 2015 03:34
-
-
Save data-doge/20245adab81ef612691a to your computer and use it in GitHub Desktop.
ways to create objects in js
This file contains hidden or 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
| // object literal notation | |
| var eugene = { | |
| name : 'eugene', | |
| age : 22, | |
| meow: function () { | |
| console.log("meow"); | |
| } | |
| }; | |
| console.log(eugene); | |
| eugene.meow(); | |
| // object constructor notation | |
| var eugene = new Object(); | |
| eugene.name = 'eugene'; | |
| eugene.age = 22; | |
| eugene.meow = function () { | |
| console.log('meow'); | |
| }; | |
| console.log(eugene); | |
| eugene.meow(); | |
| // object constructor function | |
| // this is not an object, it's a function which produces objects | |
| function Person (name, age) { | |
| this.name = name; | |
| this.age = age; | |
| } | |
| Person.prototype.meow = function () { | |
| console.log('meow'); | |
| }; | |
| var eugene = new Person('eugene', 22); // here we use our object constructor function to construct a new object | |
| console.log(eugene); | |
| eugene.meow(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment