4 ms·
From the top of my head, from someone who currently seeks work as a full stack (Rails) developer: I would ask if you by "method" mean function defined on an ob
by sbank 13y ago
From the top of my head, from someone who currently seeks work as a full stack (Rails) developer:
I would ask if you by "method" mean function defined on an object. If so, this refers to the method's object. If not, this can take on 5 values. In the global scope it is the global object; in a constructor it is the newly created object; in a function (not defined on an object - and also goes for functions defined within methods defined on objects) it is the global object; in a method (defined on object) it is the object, and finally it can be set explicitly with functions like call and apply.
How did I do? (Serious question. I think my answer is good, but I also struggle with a serious form of imposter syndrome.)
- steven777400 13y agoI did mean "function defined on an object". Your answer would have been way ahead of everyone else because no one else had an answer. :) I would also add, though, that function definition is not the critical aspect to define this. The value of this primarily varies based on how the function is called, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- davedx 13y agoNot quite right. :-) JavaScript is not truly OO. You can have a prototype method called from a callback and find that 'this' will not necessarily be the value of the object it was prototyped onto. It's really tricky, and I often have to .bind functions after I've noticed they're not working.