扩展原型对象

添加绑定函数

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>扩展原型对象</title>
</head>
<body>

</body>
</html>
<script>
    function Animal(name,color,Do) {
        this.name = name;
        this.color = color;
        this.Do = function () {
            console.log("swimming")
        }
    }
    var fish = new Animal("fish","gold");
    console.log(fish);

    Animal.prototype.smile = function () {
        console.log("-。-")
    };
    //通过prototype方式给Animal添加smile方法,其实例对象fish也能调用到新的方法
    console.log(fish.smile)
</script>

构造函数constructor绑定多个方法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>constructor</title>
</head>
<body>

</body>
</html>
<script>
    function Animal(name,color) {
        this.name = name;
        this.color = color;
        this.Do = function () {
            console.log("swimming")
        }
    }
    Animal.prototype = {
        saying : function () {
            console.log("hello")
        },
        crying : function(){console.log("呜呜呜")}
    };
var A = new Animal("fish","gold");
console.log(A);  
//{name: "fish", color: "gold", Do:ƒ() 以及一个对象,包含saying和crying}

console.log(A.Do);  
//ƒ () {console.log("swimming")}

console.log(A.saying);  
//ƒ () {console.log("hello")}

console.log(A.crying);    
//ƒ () {console.log("呜呜呜")}
</script>

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值