22-JavaScript-面向对象-构造函数-prototype

- 构造函数
- 成员函数
- prototype

1. 成员函数 的介绍


    小明 -->  人的实例

     属性: 身高 体重 ...
     行为: 吃饭 睡觉 唱歌 ... 

    成员函数就是用来描述对象的行为的

2. 构造函数 -- 描述 小明

    function Person(name, height, weight) {
        
        this.name = name;
        this.height = height;
        this.weight = weight;

        this.show = function() {
            return this.name + "  " + this.height + "  " + this.weight;
        }

        this.eat = function(food) {
            return this.name + " is eating " + food;
        }

        this.sleep = function() {
            return this.name + " like sleeping!";
        }

        this.sing = function(song) {
            return this.name + " like singing " + song;
        }

    }

    var p = new Person("小明", "170cm", "50kg");

    console.info( p.show() );

    console.info( p.eat( "apple" ) );

    console.info( p.sleep() );

    console.info( p.sing( "哇哈哈" ) );


3. 注


    ①每个Person类的实例, 都会有各自的 eat() sleep() 函数代码
        对实例属性和方法的改变, 不会影响到其他的实例.
        但, 这会造成内存的额外消耗. 如图

    ②可以使用 "==", 判断对象的方法的地址是否相等
    
    ③可使用原型的方式来使每个实例共享函数代码.

4. 给单独对象追加方法


    function Person() {
    }

 1) 方式一

    function func() {
        // do something
    }    

    var p = new Person();
    p.show = func;

 2) 方式二

    var p = new Person();
    p.show = function() {
        // do something
    };  

 3) 对象间互不影响

    function Person() {
        this.name = "123";
    }

    var p1 = new Person();
    var p2 = new Person();

    console.info( p1.name );    // 123
    console.info( p2.name );    // 123

    p1.name = "AAA";

    console.info( p1.name );    // AAA
    console.info( p2.name );    // 123

    var p3 = new Person();      
    console.info( p3.name );    // 123 


5. 让所有对象共享同一个函数 -- prototype


 1) 说明


    prototype, 相当于Java的static, 
    修饰的属性/方法是属于类的.

 2) 语法


    ClassName.prototype.propertyName = propertyValue;

    ClassName.prototype.methodName = method;

 3) 举例

    function Dog() {
        
        this.eat = function() {
            return "a dog is eating!";
        };

        Dog.prototype.bark = function() {
            return "a dog is singing!!!";
        };
    }

    var d1 = new Dog();
    var d2 = new Dog();

    console.info( d1.eat == d2.eat );   // false

    console.info( d1.bark == d2.bark ); // true

    console.info( d1.bark() );
    console.info( d2.bark() );

    // 更改模板
    Dog.prototype.bark = function() {
        return "heihei, I'm a Cat!!!!!!"
    }

    console.info( d1.bark() );
    console.info( d2.bark() );

    // false
    // true
    // a dog is singing!!!
    // a dog is singing!!!
    // heihei, I'm a Cat!!!!!!
    // heihei, I'm a Cat!!!!!!









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值