使用Call从函数继承对象

时间:2018-11-21 05:59:28

标签: javascript inheritance prototype call apply

我正在做一些测试,但不知道为什么使用调用会从另一个对象继承,例如 const objC = funcB.call(objA,'Erades'),但是我有一个对象,但是如果我从一个函数继承,我会得到一个具有有线(对我而言)行为的函数。

我不明白为什么要获取方法B,我必须做 funcC.getLastName()

如果有人可以帮助我理解这一点...

TIA

// testing Call to inherit objects / functions
    // -------------------------------------------
    
    // we declare our first function
    const funcA = function(firstName) {
      this.firstName = firstName;
      this.getFirstName = function() {
          return 'My name is ' + this.firstName;
        };
      return this;
    };
    // Create an object out of that function
    const objA = new funcA('Rodrigo');
    
    // declare second function
    const funcB = function (lastName) {
      this.lastName = lastName;
      this.getLastName = function() {
        return 'My last name is ' + this.lastName;
      };
      return this;
    };
    
    // Create an Object from funcB and ObjectA
    const objC = funcB.call(objA,'Erades');
    // We get an object
    console.log("TYPE OF: ", typeof objC)
    console.log('raw:', objC);
    console.log('method A: ', objC.getFirstName());
    console.log('prop A: ', objC.firstName);
    console.log('method B: ', objC.getLastName());
    console.log('prop B: ', objC.lastName);
    console.log('------------');
    
    // if we don't want to create an object out of a function and an object,
    // we could also inherit two functions, but the result really surprise me 
    const funcC = funcB.call(funcA,'Alonso');
    // We get a function !!!!!
    console.log("TYPE OF: ", typeof funcC);
    console.log('raw:', funcC);
    // To get result we need to do this:
    console.log('method ==>: ', funcC('Rui'));
    console.log('method A: ', funcC('Rui').getFirstName());
    console.log('prop A: ', funcC('Maria').firstName);
    console.log('method B: ', funcC.getLastName()); // looks like static method ???
    console.log('prop B: ', funcC.lastName);
    console.log('------------');

1 个答案:

答案 0 :(得分:2)

以这种方式使用call时不会继承。您正在传入一个实例,并通过一些修改相同的实例

const funcA = function(firstName) {
  this.firstName = firstName;
  this.getFirstName = function() {
      return 'My name is ' + this.firstName;
    };
  return this;
};
const objA = new funcA('Rodrigo');

const funcB = function (lastName) {
  this.lastName = lastName;
  this.getLastName = function() {
    return 'My last name is ' + this.lastName;
  };
  return this;
};

const objC = funcB.call(objA,'Erades');
// ObjC IS ObjaA
console.log(objC === objA)

使用call时,传入的对象将成为函数的this。然后,您添加一些属性并返回this,它与您刚传入的对象相同。

将函数传递给call()没什么不同。当您写时:

funcB.call(funcA,'Alonso');

您正在使用funcB作为参数调用函数Alonso。在该函数this中将引用funcA。因此,您将在指向某个函数的lastNamefuncA属性上设置一个getLastName属性,然后返回funcA,然后将其分配给变量funcC funcCfuncA指向完全相同的功能。

const funcA = function(firstName) {
  this.firstName = firstName;
  this.getFirstName = function() {
      return 'My name is ' + this.firstName;
    };
  return this;
};

const funcB = function (lastName) {
  this.lastName = lastName;
  this.getLastName = function() {
    return 'My last name is ' + this.lastName;
  };
  return this;
};


const funcC = funcB.call(funcA,'Alonso');
// the same reference
console.log(funcC === funcA)
// but when you called funcB with it, it added some properties:
console.log("funC lastname:", funcC.lastName)
// they are the same object so this also works: 
console.log("also funcA lastname:", funcC.lastName)