4

Could somebody please explain what this notation is in javascript? What is function(d) doing? In this program it seemse that x is called by the following, but I have no idea what any of this means. Thanks in advance...

x = function(d) { return d.x * width / mx; };

// later....
 x({x: .9}); // call
1

2 Answers 2

6

.9 is a value of the property x of the object(d) being passed into the function.

In the function, d = {x:9}(object) , now when you ask for d's property(x) Value (using DOT notation), it returns the value for the property x.

so d.x returns 0.9!

So you would ask me how did i pass the value of the property into the function-X in the first place, well thats what we did when we dis this -> x(objectBeingSent); where objectBeingSent is {x: .9}.

Anonymous functions are functions that are dynamically declared at runtime. They’re called anonymous functions because they aren’t given a name in the same way as normal functions.

Anonymous functions are declared using the function operator. You can use the function operator to create a new function wherever it’s valid to put an expression. For example you could declare a new function as a parameter to a function call or to assign a property of another object.

The function operator returns a reference to the function that was just created. The function can then be assigned to a variable, passed as a parameter or returned from another function. This is possible because functions are first class objects in javascript.

Here’s an example where a function is declared in the regular way using the function statement:

 function eatCake(){
     alert("So delicious and moist");
 }
 eatCake();

Here’s an example where the same function is declared dynamically using the function operator:

 var eatCakeAnon = function(){
     alert("So delicious and moist");
 };
 eatCakeAnon();

See the semicolon after the second function's closing bracket? }; You use a semi-colon after a statement. This is a statement:

var eatCakeAnon = function(){
         alert("So delicious and moist");
     };

Source

P.S. Best explanantion that i could find!

Sign up to request clarification or add additional context in comments.

9 Comments

ok, but could you please explain how the method is being called above? What is the parameter doing with the x : .9? Thanks!
@EswarRajeshPinapala No need for the second semicolon in the first example
.9 is a property of the object(d) being passed into the function. In the function, d = {x:9}(object) , now when you ask for d's property named x(using DOT notation), it returns the property x. so d.x returns 0.9!
@Mageek thanks mate, should have placed just for the statement in the second example. Edited and explained.
-1: .9 is NOT a property of any object. It is a number literal that is the same as 0.9. .9 is the value of property x of the object.
|
1

It's called an anonymous function. The function takes an object in the form of {x: number} as an argument and does some math on number.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.