JS创建对象

作者: 茜Akane | 来源:发表于2021-02-10 12:16 被阅读0次

Get Started

• 用prototype来创建对象
• 用class类来创建对象

输出12个正方形的面积和周长

• 方法:用for循环
• 问题:要创建12个对象,每个还要带2个函数当做属性,占用太多内存了
• 改进:将两个函数添加进对象的原型里,共用同一块内存
• 代码:

    let squareList = []
    let widthList = [1,2,3,4,5,6,7,8,9,10,11,12]
    let squarePrototype = {
      getArea(){
        return this.width * this.width  
      },
      getLength(){
        return this.width * 4
      }
    }
    for( let i = 0; i < 12; i++ ){
      squareList[i] = Object.create(squarePrototype)
      squareList[i].width  = widthList[i]
    }


• 改进:还可以更加精简,将代码抽离到一个函数再调用
• 代码:

    let squareList = []
    let widthList = [1,2,3,4,5,6,7,8,9,10,11,12]
    function createSquare(width){  //构造函数
      let obj = Object.create(squarePrototype )
      // 以squarePrototype为原型创建空对象
      obj.width = width
      return obj
    }
    let squarePrototype = {
      getArea(){
        return this.width * this.width  
      },
      getLength(){
        return this.width * 4
      }
    }
    for( let i = 0; i < 12; i++ ){
      squareList[i] = createSquare(widthList[i])
    }

构造函数:构造函数就是可以构造对象的函数。
封装:将逻辑细节写到一个函数里面,只需要调用传参,叫做封装。

• 改进:squarePrototype原型和createSquare函数还是分散的
• 代码:

    let squareList = []
    let widthList = [1,2,3,4,5,6,7,8,9,10,11,12]
    
    function createSquare(width){  //构造函数
      let obj = Object.create(createSquare.squarePrototype )
      // 以squarePrototype为原型创建空对象
      obj.width = width
      return obj
    }
    createSquare.squarePrototype = {  //把原型放到函数上
      getArea(){
        return this.width * this.width  
      },
      getLength(){
        return this.width * 4
      },
      constructor: createSquare  //方便通过原型找到构造函数
    }
    for( let i = 0; i < 12; i++ ){
      squareList[i] = createSquare(widthList[i])
      console.log(squareList[i].constructor)
      // constructor可以知道谁构造了这个对象
    }

注意:代码上先是定义了createSquare函数,然后给函数增加了新的属性。
当createSquare函数被调用时,将自身的squarePrototype属性指定为obj的原型,
再将obj返回赋值给目标对象。

new操作符

• 代码再优化,用new

    let squareList = []
    let widthList = [1,2,3,4,5,6,7,8,9,10,11,12]
    
    function Square(width){  //构造函数
      this.width = width
    }
    Square.prototype.getArea = function(){  //把原型放到函数上
      return this.width * this.width  
    }
    Square.prototype.getLength = function(){
        return this.width * 4
    }
    for( let i = 0; i < 12; i++ ){
      squareList[i] = new Square(widthList[i])
      console.log(squareList[i].constructor)
    }

注意:每个函数都有prototype属性,每个prototype都有constructor属性。

总结

• new X() 操作其实自动帮我们做了很多事情,这些事情包括
自动创建一个空对象
自动将该空对象的原型(_proto_)指向 X.prototype(即将 X.prototype 保存的地址复制到空对象._proto_ 里)
自动将空对象作为 this 来运行构造函数
自动 return this
• 构造函数X
X函数本身负责给对象本身添加属性
X.prototype对象负责保护对象的共用属性

代码规范

• 大小写
所有构造函数(专门用于创建对象的函数)首字母大写
所有被构造出来的对象,首字母小写
• 词性
new后面的函数,使用名词形式
如new Person(),new Object()
其他函数一般使用动词开头
如createsquare(5),createElemnet('div')

JS重要公式,也是唯一一个公式

对象.proto = 其构造函数.prototype
注意:window.object是函数对象

Circle

• 代码

fucntion Circle(radius){
  this.radius = radius
}
Circle.prototype.getArea = function(){
  return Math.pow(this.radius, 2) * Math.PI
}
Circle.prototype.getLength = function(){
  return this.radius * 2 * Math.PI
}
let circle = new Circle(5)
circle.radius
circle.getArea()
circle.getLength()

类型V.S.类

• 类型
类型是JS数据的分类,有七种
四基两空一对象
• 类
类的针对于对象的分类,有无数种
常见的有Array、Function、Date、RegExp等

数组对象

• 定义一个数组

let arr = [1, 2, 3]
let arr = new Array(1,2,3)//元素为1,2,3
let arr = new Array(3)//长度为3

• 数组对象的自身属性
'0'/'1'/'2'/'length'
注意:属性名没有数字,只有字符串
• 数组对象的共用属性
'push'/'pop'/'shift'/'unshift'/'join'
用法都在MDN

函数对象

• 定义一个函数

function fn(x, y){return x+y}
let fn2 = hunction fn(x, y){return x+y}
let fn = (x, y) => x+y
let fn = new Function('x','y','return x+y')

• 函数对象自身属性
'name'/'length'
• 函数对象共用属性
'call'/'apply'/'bind'

JS终极一问

• window是谁构造的
Window
可以通过constructor属性看出构造者
• window.Object是谁构造的
window.Function
因为所有函数都是window.Function构造的

函数都有prototype属性

如果一个对象不是函数,那么这个对象一般来说没有 prototype 属性,但这个对象一般一定会有 _proto_ 属性

class

里面引入了更多的概念

class Square{
  static x =1
  width = 0
  constructor(width){
    this.width = width
  }
  getArea(){
    return this.width * this.width
  }
  getLength(){
    return this.width * 4
  }
  get area2(){  //只读属性
    return this.width * this.width
  }
}

• 用class重写Circle

class Circle{
  constructor(radius){
    this.radius = radius
  }
  getArea(){
    return Math.pow(this.radius, 2) * Math.PI
  }
  getLength(){
    return this.radius * 2 * Math.PI
  }
}
let circle = new Circle(5)
circle.radius
circle.getArea()
circle.getLength()

相关文章

  • JS对象

    JS 创建对象 批量创建对象

  • 6.JavaScript中

    JS对象创建: JS通过构造函数创建对象: JS内置对象window: 所有的全局变量都是window的属性 所有...

  • JS笔记-006-JS对象-数字-字符串-日期-数组-逻辑

    JS对象 创建 JavaScript 对象 通过 JavaScript,您能够定义并创建自己的对象。 创建新对象有...

  • 面向对象案例:随机方块

    1.创建画布 2.创建工具对象--tools.js 3.创建box盒子对象--box.js 3.1创建构造函数 3...

  • JS创建对象

    一、基本方法 1.字面量:var o = {}; 2.构造函数:var o = new Object(); 二...

  • JS创建对象

    工厂模式 由于在ECMAScript无法创建类,所以用函数的封装以特定接口创建对象 问题:解决了创建多个相似对象的...

  • js 创建对象

    创建对象的几种方法 方法一:new Object() 方法二:{} 方法三:function xx() 为对象中添...

  • js创建对象

    this指针构造器创建 工厂模式 new 出来 prototype变量赋值 :为属性赋值链接

  • js创建对象

    新博客地址:http://gengliming.com 参考《javascript 高级程序设计(第3版)》 1、...

  • JS—创建对象

    创建object实例的方式有两种: 使用new操作符,后面跟object构造函数。 使用对象字面量表示法。 分别复...

网友评论

    本文标题:JS创建对象

    本文链接:https://www.haomeiwen.com/subject/waahxltx.html