zoukankan      html  css  js  c++  java
  • js apply

    apply:方法能劫持另外一个对象的方法,继承另外一个对象的属性。

    Function.apply(obj,args)方法能接收两个参数:

    obj:这个对象将代替Function类里的this对象;

    args:这个是数组,它将作为参数传给Function(args-->arguments)

    call和apply的意思一样,只不过是参数列表不一样。

    Function.call(obj,[param1[,param2[,...[,paramN]]]]):

    obj:这个对象将代替Function类里的this对象

    params:这个是参数列表。

    function Person(name,age) {
    this.name = name;
    this.age = age;
    }
    function Student(name,age,grade) {
    Person.apply(this,arguments);
    this.grade = grade;
    }
    var std = new Student("lai",28,"six");
    alert("name"+std.name+"
    "+"age:"+std.age+"
    "+"grade:"+std.grade);

    分析:Person.apply(this,arguments);

    this:在创建对象的这个时候代表的是student;

    通俗一点讲就是:用student去执行Person这个类里面的内容,在Person这个类里面存在this.name等之类的语句,这样就讲属性创建到了student对象里面。

  • 相关阅读:
    Spring自动代理机制
    JUnit4 详解
    struts2 OGNL
    loj4j的配置跟使用
    junit浅学笔记二
    shell变量设置
    zookeeper使用
    [zz]Linux kernel map
    glog 使用中存在的问题
    shell中特殊字符(串)
  • 原文地址:https://www.cnblogs.com/emmalai/p/5762350.html
Copyright © 2011-2022 走看看