面向对象

对象的创建

通过构造方法创建

new关键词

构造方法 他的名字是对应class名字 首字母大写

function Person(){

  console.log(this);

}

class写法:

class Person{

  constructor(){

    console.log(this);

  }

}

对象实例的创建

var person = new Person()

person.name = "hello"

工厂方法模式创建

function factor(name){

  var obj = new object()

  obj.name = name

  return obj

}

var person1 = factor("jack")

console.log(person1);

使用字面量创建

var person = {

  name:'tom',

  age:10,

  eat(){

    console.log("吃饭")

  }

}

构造函数创建过程

自动创建对象

手动添加属性

自然返回对象

工厂方法创建过程

手动创建对象

手动添加属性

手动返回对象

 

posted @ 2022-06-21 17:39  黑笑笑  阅读(23)  评论(0)    收藏  举报