我正在做一些测试,我不知道为什么如果使用调用我从另一个对象继承,比如const objC=funcB.call(objA,'Rades'(我得到了一个对象,但如果我从函数继承,我得到了具有有线(到我(行为的函数。
我不明白为什么要获得方法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
。然后添加一些属性并返回this
,它与您刚才传入的对象相同
将函数传递给call()
也没什么不同。当你写:
funcB.call(funcA,'Alonso');
您正在使用Alonso
作为参数调用函数funcB
。在该函数中,this
将引用funcA
。因此,您将在funcA
和指向函数的getLastName
上设置一个lastName
属性,然后返回funcA
,然后将其分配给变量funcC
CCD_ 15和CCD_
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)