zoukankan      html  css  js  c++  java
  • js类(继承)(一)

    //call()
    //调用一个对象的一个方法,以另一个对象替换当前对象。
    //call([thisObj[,arg1[, arg2[,   [,.argN]]]]])
    //参数
    //thisObj
    //可选项。将被用作当前对象的对象。
    //arg1, arg2, , argN
    //可选项。将被传递方法参数序列。
    //说明
    //call 方法可以用来代替另一个对象调用一个方法。call 方法可将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定的新对象。
    //如果没有提供 thisObj 参数,那么 Global 对象被用作 thisObj。
     
     
    function Person(name){    //父类
        this.name=name;
        this.SayHello=function(){alert("Hello, I'm "+this.name);};
    }
    function Employee(name,salary){   //子类
        Person.call(this,name);       //将this传给父构造函数
        this.salary=salary;
        this.ShowMeTheMoney=function(){alert(this.name+" $"+this.salary);};
    }
     
    var BillGates=new Person("Bill Gates");
    var SteveJobs=new Employee("Steve Jobs",1234);
     
    BillGates.SayHello();  //显示:I'm Bill Gates
    SteveJobs.SayHello();  //显示:I'm Steve Jobs
    SteveJobs.ShowMeTheMoney();  //显示:Steve Jobs $1234
     
    alert(BillGates.constructor == Person);   //true
    alert(SteveJobs.constructor == Employee); //true

    直接定义prototype似乎更有extends 的意韵

    function Person(name){    //父类
        this.name=name;
        this.SayHello=function(){alert("Hello, I'm "+this.name);};
    }
    function Employee(salary){   //子类
        this.salary=salary;
        this.ShowMeTheMoney=function(){alert(this.name+" $"+this.salary);};
    }
    Employee.prototype=new Person("Steve Jobs");
    var SteveJobs=new Employee(1234);
     
    SteveJobs.SayHello();  //显示:I'm Steve Jobs
    SteveJobs.ShowMeTheMoney();  //显示:Steve Jobs $1234

    文章出自:http://www.cnblogs.com/frostbelt/archive/2012/04/01/2428014.html

  • 相关阅读:
    安装LAMP服务器(Apache,MariaDB的,PHP)在CentOS / RHEL / Linux
    查看centos的内核版本,位数,版本号等信息
    linux 出现中文乱码,该如何处理?
    docker
    面试
    linux命令
    springboot整合微软的ad域,采用ldap的api来整合,实现用户登录验证、
    idea实用插件
    将lits集合转化为树状结构
    windos上安装jenkins部署springboot的jar包(未运行,只是在打包并上传linux成功了)
  • 原文地址:https://www.cnblogs.com/yichengbo/p/3719164.html
Copyright © 2011-2022 走看看