When we speak about prototypes and classes, you can always think that calling function always passing left part of calling expression (before dot) to "hidden" this argument:
- myObj.someFunc() -- myObj
- someFunc() // strict -- undefined
- someFunc() // unstrict -- window/global
But in your case, sayName
function is not associated to project, so probably you want to achieve this:
var person1 = { name: 'Chris' };
var sayName = function() { console.log(this.name) };
// you can imagine this as very simplified version of "call fn" do internally
person1.sayName = sayName;
person1.sayName(); // logs 'Chris'
It's just a programmer choice how to implement your code.
At your case function is not binded to object, so you should specify how to pass context, implicit or explicit, and passing it implicit (via left part of dot) doesn't make language less or more functional.
If you use ES6+ env. you should probably forget about the call and apply methods at most cases.
class Person {
constructor(name) {
this.name = name;
}
someFunc() {
console.log(this.name);
}
}
let person1 = new Person('Chris');
person1.someFunc(); // logs 'Chris'
let refToFn = person1.someFunc;
refToFn(); // error, because no implicit (via dot) or explicit (via arg) context passing
For example "Python" has opposite paradigm, you should always pass "this" ("self" at args) context to the method:
def someFunc(self):
print self.name;
The real question maybe you want to ask is why "this" context is exist in javascript.