After reading about the first class object i decided to make my new first-class object like a function but with another name. i searched and found many thing about functions in javascript.
1) that every function is an object and can also be treated like an object.
look here on code:
var myfunc3 = new Function(
"a", " return a + 1;");
from page this
as far as i know that any function defined before can be called by using new
constructor that is:
var person = function(name, age){
this.name=name;
this.age = age;
}
now i can call this by using new
that is:
var x = new person("tom", 34);
so from the above example i can say that a function defined earlier and then with new
can be called again!
1) how function uses its round brackets,
2) cannot understand the line Function( "a", " return a + 1;");
3) How can we implement function?
i know you will surely dislike this question but i want to do this because i want to understand every expact of JavaScript.
Thanks!
1) This is the syntax for calling functions, defined in the language.
f(a); // calls the function "f" with the argument "a"
2) Functions are first-class objects, meaning they can be passed around just like any other object and like other objects they have a constructor, Function
. The Function
constructor creates a new function by evaling the code in a new isolate scope, so you don't have access to the surrounding variables. Again, the Function
is a form of eval
, avoid it, this is not the way to create functions in JavaScript.
3) You don't implement a function. A function is an object that is already implemented. There are two ways of creating functions in JavaScript. A function declaration:
function f(){}
And a function expression:
var f = function(){};
The difference is about hoisting. See var functionName = function() {} vs function functionName() {}