构造函数

  • 构造函数的概念

    • 任何函数都可以当成构造函数
    • 只要把一个函数通过new的方式来进行调用,我们就把这一次函数的调用方式称之为:构造函数的调用
      • new CreateFunc(); 此时CreateFunc就是一个构造函数
      • CreateFunc(); 此时的CreateFunc并不是构造函数
    • new Object()等同于对象字面量{}
  • 构造函数的执行过程

    • var p1=new Person();
    • 创建一个对象 (我们把这个对象称之为Person构造函数的实例)- _p1
    • 创建一个内部对象,this,将this指向该实例(_p1)
    • 执行函数内部的代码,其中,操作this的部分就是操作了该实例(_p1)
    • 返回值:
      • 如果函数没有返回值(没有return语句),那么就会返回构造函数的实例(_p1)
      • 如果函数返回了一个基本数据类型的值,那么本次构造函数的返回值是该实例(_p1)
      • 如果函数返回了一个引用数据类型的值,那么本次函数的返回值就是该值
  • 创建构造函数

    var student = {};
    student.name = 'sunny';
  • 内置的构造函数
    var obj = new Object();
    var num = new Number();
    var str = new String();
    var now = new Date();
    var arr = new Array();
    var reg = new RegExp();
    var bool = new Boolean();
    var func = new Function();
    var img = new Image();


    console.log(obj);
    console.log(obj.constructor);

    console.log(num);
    console.log(num.constructor);

    console.log(str);
    console.log(str.constructor);

    console.log(now);
    console.log(now.constructor);

    console.log(arr);
    console.log(arr.constructor);

    console.log(reg);
    console.log(reg.constructor);

    console.log(bool);
    console.log(bool.constructor);

    console.log(func);
    console.log(func.constructor);

    console.log(img);
    console.log(img.constructor);
  • 自定义构造函数
    • 构造函数的命名推荐采用帕斯卡命名规则,即所有的单词首字母大写。
    • 在构造函数内部,使用this来表示刚刚创建的对象。
    var Computer = function(memory, storage, videoMemory, run){
        this.memory = memory;
        this.storage = storage;
        this.videoMemory = videoMemory;
        this.run = run;
        this.introduce = function(){
            return this.memory;
        }
    }
    var noteBook = {
        memory: '16GB',
        storage: '2TB',
        videoMemory: '4GB',
        run: function(){
            return '你好,世界!';
        }
    }
    var Dell = new Computer(noteBook.memory, noteBook.storage, noteBook.videoMemory, noteBook.run);
    console.log(Dell);
    console.log(Dell.run());
    console.log(Dell.introduce());
  • 私有成员
    • 在构造函数中,使用var关键字定义的变量称为私有成员
    • 在实例对象后无法通过“对象成员”的方式进行访问
    • 但是私有成员可以在对象的成员方法中访问
    var Computer = function(memory, storage, videoMemory, run){
        var processor = 'Core i7-4700MQ';
        this.memory = memory;
        this.storage = storage;
        this.videoMemory = videoMemory;
        this.run = run;
        this.introduce = function(){
            return this.memory + processor;
        }
    }

    var noteBook = {
        memory: '16GB',
        storage: '2TB',
        videoMemory: '4GB',
        run: function(){
            return '你好,世界!';
        }
    }
    var Dell = new Computer(noteBook.memory, noteBook.storage, noteBook.videoMemory, noteBook.run);
    console.log(Dell);
    console.log(Dell.processor);
    console.log(Dell.run());
    console.log(Dell.introduce());
  • 构造函数中的return关键字
    • 构造函数中,return返回一个数组或对象等引用类型数据,则构造函数会直接返回该数据,而不会返回原来创建的对象。
    • 如果返回的是基本类型数据,则返回的数据无效,依然会返回原来创建的对象。
    • 总结:构造函数中的return只能返回引用类型的数组或对象,有合规的返回值返回数据,没有就返回本身的对象数据
    // 返回值是引用类型数据,返回对象本身
    var Computer = function(memory, storage, videoMemory, run){
        var processor = 'Core i7-4700MQ';
        this.memory = memory;
        this.storage = storage;
        this.videoMemory = videoMemory;
        this.run = run;
        this.introduce = function(){
            return this.memory + processor;
        }
        return {say:'你好,世界!'};
    }

    var Lenovo = new Computer();
    console.log(Lenovo);
    // 返回值是基本类型数据,返回数据无效,返回原来创建的对象
    var Computer = function(memory, storage, videoMemory, run){
        var processor = 'Core i7-4700MQ';
        this.memory = memory;
        this.storage = storage;
        this.videoMemory = videoMemory;
        this.run = run;
        this.introduce = function(){
            return this.memory + processor;
        }
        return 0;
    }

    var Lenovo = new Computer();
    console.log(Lenovo);
原文地址:https://www.cnblogs.com/SharkJiao/p/13548202.html