使用 Call 从函数继承对象

using Call to inherit objects from a function

我正在做一些测试,我不知道为什么如果使用 call 我从另一个对象继承,比如 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('------------');

当您以这种方式使用 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。然后添加一些属性和 return this,这是您刚刚传入的同一对象。

将函数传递给 call() 也不例外。当你写:

funcB.call(funcA,'Alonso');

您正在使用 Alonso 作为参数调用函数 funcB。在该函数中 this 将引用 funcA。所以你会在 funcAgetLastName 属性 上设置一个 lastName 属性 指向一个函数,然后 return funcA 这是然后赋值给变量funcCfuncCfuncA指向完全相同的函数。

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)