Skip to content

Instantly share code, notes, and snippets.

@hjzheng
Created July 3, 2014 11:47
Show Gist options
  • Save hjzheng/118059e0914dd36aaeec to your computer and use it in GitHub Desktop.
Save hjzheng/118059e0914dd36aaeec to your computer and use it in GitHub Desktop.
类比普通属性(方法)和原型属性(方法)
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