资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

javascript继承方法有什么

本篇内容介绍了“javascript继承方法有什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

创新互联公司长期为近1000家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为曲阳企业提供专业的网站制作、成都做网站曲阳网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。

javascript继承方法有:1、使用call()方法,可以编写能够在不同对象上使用的方法;2、apply()方法,语法“apply(用作 this 的对象,要传递给函数的参数的数组)”。

javascript继承方法有什么

本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。

1、call() 方法

call() 方法是与经典的对象冒充方法最相似的方法。它的第一个参数用作 this 的对象。其他参数都直接传递给函数自身

function Huster(name,idNum,college)
    {        this.name = name;        
        this.idNum = idNum;        
        this.college = college;        
        this.course = new Array();    
        
        this.addCourse = function(course)//这个方法不能用prototype来定义,如果用的话,子类无法继承该方法
        {                   //用原型prototype定义的方法可以用原型链来继承,call()方法和apply()方法都无法继承            this.course.push(course);
            console.log(this.course);
        };    
            
    }    
    
    function Autoer(name,idNum)
    {        
    this.college = ‘自动化‘;
        Huster.call(this,name,idNum,this.college);//Autoer使用call()方法来继承 Huster
     if (typeof Autoer._initialized == "undefined") 
     { 
      Autoer.prototype.sayHobby = function() //自己的方法可以用原型链prototype定义
      { 
        alert(this.college+‘人喜欢撸代码!‘);     
      }; 
      Autoer._initialized = true; 
     } 
  } 
   
  var autoer1 = new Autoer(‘偶人儿‘,‘U123456789‘); //声明一个实例autoer1
  console.log(autoer1.name,autoer1.idNum,autoer1.college,autoer1.course); 
  autoer1.addCourse(‘logistics‘);//调用Huster的方法
  autoer1.sayHobby();    //调用自身的方法

2、apply() 方法

apply() 方法与call()方法几乎一样,唯一的区别就是apply()方法只有两个参数,第一个用作 this 的对象,第二个是要传递给函数的参数的数组。也就是说apply()方法把call()方法的若干个参数放到一个数组里,传递给父类

function Huster(name,idNum,college){    
this.name = name;            
this.idNum = idNum;    
this.college = college;    
this.course = new Array();            
this.addCourse = function(course)//这个方法不能用prototype来定义,如果用的话,子类无法继承该方法    
{                   //用原型prototype定义的方法可以用原型链来继承,call()方法和apply()方法都无法继承        
this.course.push(course);        
console.log(this.course);    
};                
}            
function Autoer(name,idNum)    
{        
this.college = ‘自动化‘;        
Huster.apply(this,new Array(name,idNum,this.college));//Autoer使用apply()方法来继承 Huster     
if (typeof Autoer._initialized == "undefined")      
{       
Autoer.prototype.sayHobby = function() //自己的方法可以用原型链prototype定义      
{         
alert(this.college+‘人喜欢撸代码!‘);           
};       
Autoer._initialized = true;      
}   
}      
var autoer1 = new Autoer(‘偶人儿‘,‘U123456789‘); //声明一个实例autoer1  
console.log(autoer1.name,autoer1.idNum,autoer1.college,autoer1.course);   
autoer1.addCourse(‘logistics‘);//调用Huster的方法  
autoer1.sayHobby();    //调用自身的方法

“javascript继承方法有什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


文章标题:javascript继承方法有什么
URL分享:http://cdkjz.cn/article/iiochj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220