Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

javascript - 对象 #46

Open
threetown opened this issue Aug 27, 2018 · 0 comments
Open

javascript - 对象 #46

threetown opened this issue Aug 27, 2018 · 0 comments

Comments

@threetown
Copy link
Contributor

一、创建Object实例的方式

  • A、使用 new 操作符后跟 Object 构造函数,如:
new Object()
  • B、字面量
{}

二、创建对象

A、工厂模式

  • 已解决:创建多个对象的问题
  • 未解决:对象识别
function createPerson(){
  var o = new Object();
  o.name = name;
  o.say = function(){
    console.log(this.name)
  }
}

var p1 = createPerson();

B、构造函数模式

  • 问题:每个方法都要在每个实例上重新创建一遍
function Person(name){
  this.name = name;
  this.say = function(){
    console.log(this.name)
  }
}
var p1 = new Person('leon');

C、原型模式

创建的每个函数都有一个 prototype 属性,这个属性是一个指针,指向一个对象。

function Person(){

}
Person.prototype.name = 'leon'
var p1 = name Person()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant