Last active
August 29, 2015 14:04
-
-
Save wchaowu/ad89d5c856d318f45102 to your computer and use it in GitHub Desktop.
对象的创建
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
| //静态创建方式,json对象 | |
| /* | |
| var person = { | |
| name: '', | |
| age: '', | |
| eat1 :function (){ | |
| } | |
| }*/ | |
| //对象构成的创建类 | |
| var person = function (){ | |
| this.name = ''; | |
| this.age = ''; | |
| this.eat2 = function (){ | |
| } | |
| } | |
| /* | |
| 设置对象的属性 | |
| */ | |
| person.getName = function () { | |
| return this.name; | |
| } | |
| person.setName = function (argName) { | |
| this.name = argName; | |
| console.log(this) | |
| } | |
| person.setName('111'); | |
| obj = new person(); | |
| console.log(obj) | |
| console.log(person.name + '222'); | |
| var b= person; | |
| b.name="333"; | |
| console.log("b"+person.name) |
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
| function Person(name, age) { | |
| this.name = name; | |
| this.age = age; | |
| /* | |
| 在对象里面创建 | |
| this.getName= function (){ | |
| return this.name; | |
| } | |
| */ | |
| } | |
| /*基于原型的创建*/ | |
| Person.prototype = { | |
| getName: function() { | |
| return this.name; | |
| }, | |
| getAge: function() { | |
| return this.age; | |
| } | |
| } | |
| /* Instantiate the class. */ | |
| var alice = new Person('Alice', 93); | |
| var bill = new Person('Bill', 30); | |
| console.log(bill.getName()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment