objet javascript orienté (OOP) détaillée

Vue d' ensemble orienté objet:
sur un code abstrait, l' interface d'appel unifié externe fournit des idées de programmation.
Objet : résumé collection non ordonnée des propriétés et méthodes de
orienté objet basé sur un prototype: mode orienté objet basé sur un prototype, l'objet est les constructeurs ont confiance prototype construit en utilisant l'
objet est le parent est la racine de javascript
prototype: chaque fonction aura une propriété prototype, cette propriété est une adresse de mémoire pointant vers l'objet, qui stocke l'adresse de mémoire d'un objet
instanceof: détecter si oui ou non l'objet appartenant à une instance particulière d'une classe

constructeur de l' objet:
var obj = new nouvelle fonction (var1, var2, ..., FunctionBody ());
Note: faible efficacité des objets construits de cette manière, l'objet créé de cette manière est appelée l'objet de fonction, d' objets autres appel général par
exemple:

  var obj=new Function("a","b","return a+b") //构造一个函数对象
      var sun=obj(2,9)
      console.log(sun)

6 façons de créer des objets

    // 第一种  字面量方式创建对象
    var obj = {
      name: "qjj",
      age: 18,
      getName: function () {
        console.log(this.name)
      }
    }
    obj.getName()

    //第二种 使用new关键字 new一个对象  new Object
    var person = new Object()
    person.name = "qjj",
      person.height = 178,
      person.getHeight = function () {
        console.log(this.height)
      }
    person.getHeight()

    //第三种 构造函数模式创建对象
    function car(color, width) {
      this.color = color
      this.width = width
      this.getCor = function () {
        console.log(this.color)
      }
    }
    var bao = new car("red", 156)
    bao.getCor()

    //第四种 工厂模式创建对象
    function musicer(longer, dancer) {
      var obj = new Object()
      obj.longer = longer
      obj.dancer = dancer
      obj.getLonger = function () {
        console.log(this.longer)
      }
      return obj
    }
    var musicer1 = musicer(1554, "胡歌")
    musicer1.getLonger()


    //第五种 原型模式创建对象

    function producter() {

    }
    // producter.prototype.color = "blue"
    // producter.prototype.height = 555
    // producter.prototype.getHeig = function () {
    //   console.log(this.color)
    // }
    producter.prototype = {
      color: "blue",
      height: 555,
      getHeig: function () {
        console.log(this.color)
      }
    }

    var producter1 = new producter()
    producter1.getHeig()



    //第六种 混合模式创建对象
    function test(color, height, width) {
      this.color = color;
      this.height = height;
      this.width = width;
      this.getInfo = function () {
        console.log(this.color, this.height, this.width)
      }
    }
    test.prototype = {
      shope: "big"
    }

    var car1 = new test("red", "100", "666")
    car1.getInfo()
    console.log(car1.shope)
Publié 25 articles originaux · louanges gagnées 0 · Vues 638

Je suppose que tu aimes

Origine blog.csdn.net/JamesHKK/article/details/104550704
conseillé
Classement