-
Notifications
You must be signed in to change notification settings - Fork 0
Home
This repository is part of a larger project!
Javascript has the method call which allows the user to give an object temporarily a method. An example could look like as follows:
function method(a,b)
{
return alert(a + b);
}
//Receiver object
var Receiver = {};
//Custom receiver - outputs "Hello World!"
method.call(Receiver,"Hello ", "World!");
//Uses the global object as receiver - outputs "Second Hello!"
method.call(this,"Second ", "Hello!");
The object which gets temporarily a method/function is also known as the receiver object. It receives the function call or calls the function.
Javascript has a range of inbound methods which could be overwritten, if a developer desires so by intention or accident. The challenge here is that the lack of knowledge, of those method names, rises the risk of overwritting them.
To counteract this, Javascript offers the method call.
Also with call, a method could call the properties of the receiver object, like the following code example should explain:
function method()
{
//"this" means "Receiver" and "Property"
//is the specific property name of the receiver object "Receiver"
return this.Property;
}
var Receiver =
{
Property: "Hello World!"
};
//Outputs - "Hello World!"
method.call(Receiver);
The method call could also be used for higher-order functions. An example should be found in the file "call.js".
In that file the Shelf object`s method giveAll is defined as a higher-order function.
The user interaction part should look like the content as seen below by starting "index.html" in a web browser.
-
By using the left mouse
🅱️ utton on the objects in the shelf picture, the objects are put into the cart. -
🅱️ utton "TAKE ALL" puts all the seen objects into the cart at once -
🅱️ utton "RESET" deletes all objects which could be found in the cart
The colored areas are just for a better readability in the wiki and are not part of the content. To use the project just download the files and execute "index.html". Note that all files should be placed in the same folder so that the functionality of the code is guaranteed.
This knowledge was gained:
-
Effective JavaScript "68 Specific Ways to Harness the Power of JavaScript" by David Herman
-
Two ways of clearing an array in JavaScript by Dr. Axel Rauschmayer
Sources: