Using this for object references [duplicate] - javascript

This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 7 years ago.
JavaScript and I believe all other OO languages have a special keyword, this, that you can use within a method to refer to the current object. For example, suppose you have a function called validate that validates an object's value property, given the object and the high and low values
function validate(obj, lowval, hival) {
if ((obj.value < lowval) || (obj.value > hival))
alert("Invalid Value!");
}
Then, you could call validate in each form element's onchange event handler, using this to pass it the element, as in the following example:
<input type="text" name="age" size="3"
onChange="validate(this, 18, 99)">
In general, this refers to the calling object in a method.
I understand all of this usage, I just have a little question: how this works under the hood? I mean how does the method know which object is calling if you dont specify the name?

In compilers I have worked with, this is done by passing an implicit parameter on the call stack along with the explicit parameters. Of course it may vary based on the compilation or interpretation engine, but this is the simplest approach.

Related

Seeking an alternative to arguments.callee.name for use in console.log [duplicate]

This question already has answers here:
How to get function name in strict mode [proper way]
(2 answers)
argument.callee.name alternative in the new ECMA5 Javascript Standard [duplicate]
(4 answers)
Closed 5 years ago.
'use strict';
function foobar(){
console.log(arguments.callee.name); // foobar
}
I hear this once worked, but now it's deprecated. There seem to be other alternative to it for other situations, and I've seen lots of references to using function.name, but having to enter foobar.name defeats the purpose. Within a class I can use this.constructor.name and get the name of the class. I'm looking for something like that for the name of a function.
Why this is not a duplicate to any existing question since ES6: one answer to one similar question talks about using named functions, but this is a named function. To another question the answer is to use function.name - but this question is basically asking if there is a way to .log the name without knowing the name. To another question the answer was to use something like ()=>Function.caller.name but that is non-standard.
If you're using ES6, the function now has a property 'name'
Using your example:
function foobar(){
console.log(foobar.name); // foobar
}

What does `bind(this)` mean? [duplicate]

This question already has answers here:
What is the use of the JavaScript 'bind' method?
(23 answers)
Closed 6 years ago.
In some part of onInit function of a controller in a SAPUI5 application there is an auto generated code like this:
this.getView().addEventDelegate({
onBeforeFirstShow: function() {
// Some codes
}.bind(this)
});
Now my question is what does .bind(this) mean? What does it do? Is it a pure JavaScript code or it is related to SAPUI5?
Yes, it's pure JavaScript code, you can learn more about what bind is and does here
The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.
In this case what it does is basically binding the current this to that function, so when onBeforeFirstShow is called, the this inside that function will be the one passed to the bind function.
You may also want to look at the new arrow function syntax in ES6, it auto binds the current this so bind(this) is not necessary.
It binds the listener of the function to the current class, then when you use this pointer inside of the onBeforeFirstShow function, the this pointer refer to encapsulated class and you can access to its members.
The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.
Syntax
fun.bind(thisArg[, arg1[, arg2[, ...]]])
Reference to Mozilla Developer Network

Javascript use variable name to thread a function? [duplicate]

This question already has answers here:
Use JavaScript variable as function name?
(5 answers)
Closed 7 years ago.
In GSC, you are able to make a variable become the name of a function that you thread. It looks like this:
variable = "pizza";
[[variable]]();
the engine then reads it like:
pizza();
my question is, is it possible to do that in javascript as easily or do I have to make if/else/switch statements for it?
my question is, is it possible to do that in javascript as easily or
do I have to make if/else/switch statements for it?
If you want to use the safe, fail-proof way, then you can access such variables only in two contexts.
If the variable is in global context, in the case of which, you can do window[variable]();
Else if the variable is a property of an object, in the case of which, you can do obj_name[variable](), basically anything that can be accessed via bracket notation. window is an object too.
Then there's always the dirty way:
You can use highly evil eval like eval(variable + "()") or you can use the Function constructor in the same way. Note however that both the methods can be misused and are highly advised against.

Passing named arguments to a Javascript function [duplicate]

This question already has answers here:
Is there a way to provide named parameters in a function call in JavaScript?
(12 answers)
Closed 7 years ago.
Calling a Javascript function with something like
someFunction(1, true, 'foo');
is not very clear without familiarity with the function.
I have seen and used the style where comments are inserted to name the arguments:
someFunction(/*itemsToAdd*/1, /*displayLabel*/ true, /*labelText*/ 'foo');
But when it gets beyond 3 or more parameters, it seems better to pass arguments in a JSON object which makes it order-independent, and allows default values to be provided in the called function
someFunction({'itemsToAdd':1, 'labelText':'foo', 'displayLabel':true});
My question is; what is the general practice in the industry, and are there overriding reasons for not using any of these methods. Lint for example does not like the second method.
Personally I'd just grep the function name and take a look at the comment associated with it. Well maintained code will have a comment above the function explaining what the arguments are and what it does with them, and you can just paste that above the function call if you need to explain why your arguments are the way they are.
Using a JSON to pass arguments seems like a way to add unnecessary parsing overhead and to possibly confuse the maintainer - just add more fields and pass in NULLs to the fields where you want default values, and you can explain why you're passing in NULLs in the call comment instead of just having them not appear in the JSON.

Is there a way to turn a JS object into a function? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to convert an "object" into a function in JavaScript?
I have a theoretical question. As far as I know, {} objects come from Object, and functions inherit from that as well. Is there something user-accessible that can make {} objects callable, like functions, or is it interpreter magic?
I'm thinking of something like:
var myobj = {}
myobj["__call"] = function() {do_things();}
myobj();
and have it work. Is it possible? If not, why not?
Thanks!
Is there a way to turn a JS object into a function?
No.
All functions are objects, but not all objects are functions. What is the meaning of "calling" an object, if it's not already a function?
Related: What is the difference between a function object and a callable object?
From my comment below (since this seemed particularly useful to the OP):
A function is callable if it has the internal [[Call]] method. See ECMA-262 §9.11 and §13.2.1, and Table 9 on p.34. But, AFAIK, there's nothing you can do to add the [[Call]] method to an object that doesn't already have one.

Categories

Resources