Created
July 3, 2014 11:47
-
-
Save hjzheng/118059e0914dd36aaeec 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
function Foo(){ | |
this.bar = "bar";//普通属性 | |
} | |
Foo.prototype.bar = "prototype bar"; //原型属性 | |
var f = new Foo(); | |
f.bar //"bar" | |
使用类比记忆 | |
this.bar = "bar" 比作样式style | |
Foo.prototype.bar = "prototype bar" 比作class | |
主要区别: | |
1.优先级? 普通属性(方法)优先级高 | |
2.是否共用一份? 原型属性(方法)共用一份,省内存,所以共用的属性和方法最好使用prototype |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment