js构造函数

在学习构造函数之前我们先来了解this指向
this关键字
this无法进行赋值
全局作用域中,this指向window
构造函数中的this指向当前实例化的具体的对象(谁调用this所在的函数,那么this就指向谁)
谁调用this所在的函数,那么this就指向谁

什么是构造函数
用 new 关键字来调用的函数,称为构造函数。
注意:构造函数首字母一般大写(大驼峰命名)

在我们印象中一共有三种创建方式
第一种
var obj={}
字面量的创建

第二种

  function hanshu(){
            return {a:1,b:2};
        }
        var a = hanshu();
        console.log(a);

函数的创建

第三种

var obj=new Object()

new关键字创建
其实也是构造函数创建与函数创建有一定相似

function gouzhao(name,age){
            this.name = name;
            this.age = age;
        }
        var a = gouzhao('xjs',21);
        console.log(a);

注意事项
1.构造函数的函数名使用大驼峰命名 人为定义的
2.构造函数内部,会自动创一个空对象,this执行这空对象,并且会自动返回这个对象
3.构造函数中,如果出现return 如果是一个基本数据类型值,直接忽略,如果是引用类型,返回引用类型
4.所有的对象,最终都是通过构造函数创建的

然后我们学习一下new.target(es6中的)
它在构造函数中使用,用来检测该函数是否为构造函数
当new.target==undefined时该函数不为构造函数

实例
使用new.Target实现无论是否使用new调用函数都返回一个对象

function Aaa(name, age) {
            if (new.target == undefined) {
                return {
                    name: name,
                    age: age
                }
            } else {
                this.name = name
                this.age = age
            }
        }
        // var aa = new Aaa("xjs", 21)
        var aa = Aaa("xjs", 21)
        console.log(aa);

这就是new.Target的用法,

然后我们也用构造函数来写两个例子
第一题
创建一个构造函数实现计算器的功能(+,-,*,/) 清零;

     function Bbb() {
            this.jieguo=0
            this.jia = function (a, b) {
                return this.jieguo= a + b
            }
            this.jian = function (a, b) {
                return this.jieguo=a - b
            }
            this.cheng = function (a, b) {
                return this.jieguo=a * b
            }
            this.chu = function (a, b) {
                return this.jieguo=a / b
            }
        }
        var bbb = new Bbb()
        console.log(bbb.jian(1,2));
        console.log(bbb.jia(1,2));
        console.log(bbb.cheng(1,2));
        console.log(bbb.chu(1,2));
        console.log(bbb);

第二题

 // 编写一个构造函数
        /* 
            具有health 默认值是50
            具有attack() 被击打后 生命值随机降低(5-10)
            具有injured() 参数为收到的伤害值,当伤害值大于生命值 输出'侥幸生存'
                                            当伤害值小于生命值 输出'你死了'
        */
        // function Hanshu() {
        //     //默认值
        //     this.health = 50
        //     //默认血量减去随机伤害(剩下血量)
        //     this.suiji=function(){
        //        return this.health - Math.round(Math.random() * 10)
        //     }    
        //     //将剩下血量赋值
        //     this.attack = function () {
        //         this.health = this.suiji()
        //     }
        //     //判断是否死亡
        //     this.injured = function () {
        //         if (this.health<0){
        //             console.log("你死了");
        //         }
        //         else{
        //             console.log("侥幸生存");
        //         }
        //     }
        // }
        // var aaa = new Hanshu()
        // console.log(aaa);
        // aaa.injured()
        // console.log(aaa);
        // aaa.injured()
        // console.log(aaa);
        // aaa.injured()
        // aaa.injured()
        // aaa.injured()
        // aaa.injured()
        // aaa.injured()
        // console.log(aaa);

好了今天的构造函数就学到这里

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值