Call a Javascript function stored in an object property [duplicate] - javascript

This question already has answers here:
JavaScript function aliasing doesn't seem to work
(6 answers)
Closed 8 years ago.
How come the code below doesn't work?
var x = {};
x.a = alert;
x.a('asdf'); // TypeError: Illegal invocation

Because the internals of the alert function requires that the value of this be window.
x.a.call(window,'asdf');
… will work.

Related

Function expression call in JavaScript [duplicate]

This question already has answers here:
JavaScript object functions and `this` when unbound and returned in expression/parens
(2 answers)
Closed 3 years ago.
I came across this (document.createElement)('a');.
How is it different from document.createElement('a');
No difference.
Please read doc for more.

Deconstructing property from object throws error [duplicate]

This question already has answers here:
How to bind methods when destructuring an object in JavaScript?
(5 answers)
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 4 years ago.
This code in ES6:
var myObj = {firstname: "Stevey",
printFirstName() {
console.log(this.firstname);
}}
function printNames({printFirstName}) {
printFirstName();
}
printNames(myObj);
TypeError: Cannot read property 'firstname' of undefined. How to fix this?
Edit: This question is not a duplicate. The code in 'exact' answer marked, does not take function from deconstructed object as function param. And the other answer marked is an essay on how 'this' works. That is just a reference not the direct answer.

Calling a jQuery function using a variable name [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 7 years ago.
I have the following code where MODE is a variable. Its value should be a method of jQuery's tinycolor liberary.
$cor = tinycolor($cor).mode(z).toString();
I'd like to call that method on that line so, for instance, when mode = 'lighten' $cor would be
$cor = tinycolor($cor).lighten(z).toString();
Is there a way of doing it this way?
Thanks!
Use bracket notation
$cor = tinycolor($cor)[mode](z).toString();

Why javascript return Uncaught ReferenceError [duplicate]

This question already has answers here:
ReferenceError and the global object
(3 answers)
Closed 7 years ago.
Why javascript throws Uncaught ReferenceError when trying to get global variable directly for example someVar, but when trying get it through window.someVar it gots undefined
Please post an example of your code. Global variables should be accessible on the window object for example:
var someValue = "foo";
function alertsomeValue(){
alert(window.someValue);
}
alertsomeValue();
//alerts "foo"

How to get the prototype property in Javascript? [duplicate]

This question already has answers here:
Why is JavaScript prototype property undefined on new objects?
(5 answers)
How does JavaScript .prototype work?
(26 answers)
Closed 7 years ago.
I define an object using function
AAA = function ( x) {
this.x = x || 0;
};
Then I created on object using
var a = new AAA();
If I am using a.prototype, I get "undefined". I can only use AAA.prototype to access the prototype property, I would like to know why?

Categories