zoukankan      html  css  js  c++  java
  • 创建对象的一种方式&一种继承机制(代码实例)

                /* 创建对象的一种方式:混合的构造函数/原型方式,
                 *用构造函数定义对象的所有非函数属性,用原型方式定义对象的函数属性(方法)
                 */
                function People(sname){
                    this.name = sname;
                }
                People.prototype.sayName = function(){
                    console.log(this.name);
                }
                /* 一种继承机制:
                 * 用对象冒充继承构造函数的属性,用原型prototype继承对象的方法。
                 */
                function Student(sname,sage){
                    People.call(this,sname);
                    this.age = sage;
                }
                //临时构造函数模式(圣杯模式)
           var F = new Function(){} ;
           F.prototype = People.prototype ;
           Student.prototype = new F() ;
           Student.prototype.sayAge = function(){
                    console.log(this.age);
                };
          //实例
          var people1 = new People("jeff");
          people1.sayName(); //输出:jeff
          var student1 = new Student("john",30);
          student1.sayName(); //输出:john
          student1.sayAge(); //输出:30
  • 相关阅读:
    jQuery操作CheckBox的方法(选中,取消,取值)详解
    checkAll操作
    java 去掉重复的数字
    multiselect多选下拉框
    toggle() 隐藏和收缩
    Test 6.29 T4 简单数据结构练习
    Test 6.29 T3 小学生
    Test 6.29 T2 染色
    Test 6.29 T1 预算方案
    [洛谷P3338] ZJOI2014 力
  • 原文地址:https://www.cnblogs.com/cag2050/p/5463935.html
Copyright © 2011-2022 走看看