构造函数

2023-02-07  本文已影响0人  败于化纤

⽇考题

⼀、什么是构造函数(10分)

⼆、简述构造函数的⼯作原理(20分)

三、使⽤new调⽤构造函数创建实例对象的问题是什么(10分)

四、我想为实例对象(构造函数:Car())添加⼀个 start(){console.log('启动')} ⽅法,请写出语句(10分)

Car.prototype.start = function(){console.log('启动')}

五、请阐述你对”对象的原型式图“的理解(50分)

1.什么是构造函数

2.创建一个User构造函数

创建一个构造函数需要两步

从技术上讲,构造函数是具有以下约定的常规函数:

创建一个User对象的模板函数

        function User(){
            this.nema = "长舟";
            this.Nickname = "十恶不赦";
            this.age = "10";
            this.gender = "女";
            this.sing = function(){console.log("晚夜微雨问海棠")};
        }

使用new调用构造函数

  const User1 = new User();
        const User2 = new User();
        console.log(User1) ;
        console.log(User2) ;

传参

 function User(nema,Nickname,age,gender,sing){
            this.nema = nema;
            this.Nickname = Nickname;
            this.age = age;
            this.gender = gender;
            this.sing = function(){console.log("晚夜微雨问海棠")};
        }

传入实参

const User1 = new User("白器","华中",18,"女");
        const User2 = new User("份","华中",300,"男");
        console.log(User1);
        console.log(User2);

3.构造函数工作原理

基本上,使⽤new调⽤构造函数时执⾏了以下操作:

从技术上,构造函数在内部做隐式做了如下工作

 function User(nema,Nickname,age,gender,sing){
//this 默认指向window
//this = {} 1.隐式:偷偷修改了this指向一个空对象
            this.nema = nema; //2.显式:为空对象添加属性和方法
            this.Nickname = Nickname;
            this.age = age;
            this.gender = gender;
            this.sing = function(){console.log("晚夜微雨问海棠")};
        }
//return this 3. 隐式:设置return this

为什么使用构造函数会创建一个对象呢?

过程复杂且不利于维护。使⽤构造器函数可以解决这个问题

构造函数里面的return

 function User(nema, Nickname, age, gender, sing) {
            //this 默认指向window
            //this = {} 1.隐式:偷偷修改了this指向一个空对象
            this.nema = nema; //2.显式:为空对象添加属性和方法
            this.Nickname = Nickname;
            this.age = age;
            this.gender = gender;
            this.sing = function () { console.log("晚夜微雨问海棠") };
             //return this 3. 隐式:设置return this
            // return "123"//如果返回非对象,那么正常返回this
            return {x:1}//如果返回对象,那么返回的对象将覆盖掉this
        }
            const uu =new User("bzd","ai")
            console.log(uu)

构造函数的问题

问题:通过构造器函数向对象添加⽅法的缺点是:当创建多个对象实例时, this.属性或方法被添加到每⼀个实例对象中,也就是说内存使⽤效率降低:每个实例的 属性或⽅法会在内存堆中创建存储空间。
解决:使用prototype:把⽅法添加到构造函数的 prototype 对象身上,让所有的对象实例共享该⽅法或者属性。

把⽅法添加到构造函数的原型对象上

示例

let student3 = new Student('⼩丽',16)
student3.sayHi() //⼩丽:'Hi~'
function User(name, age) {
this.name = name;
this.age = age
// this.sing = function () {console.log('lalala')} //错误⽅法:不要把函数添加到构造函数内
部,原因:消耗内存
}
User.prototype.sing = function () {console.log('lalala')}//正确⽅法:要把⽅法添加到构造函数的
原型对象上。
console.log(User);
const user1 = new User('张三', 19)
const user2 = new User('张三', 19)
console.log(user1.sing === user2.sing);//true 说明user1.sing和user2.sing指向同⼀个函数
// console.log(User.prototype);
// console.log(user1.toString());//[object Object]
console.log(user1.__proto__);

原型示意图

3262e8231b0cfd5957e4dfe42d46e54.png

阐述

构造函数的prototype属性是什么

constructor

--proto-- 属性

null

上一篇 下一篇

猜你喜欢

热点阅读