So the short version, what I don't understand is this line of code:
(new Function("paper", "window", "document", cd.value)).call(paper, paper);
The long version, look at these functions:
window.onload = function () {
var paper = Raphael("canvas", 640, 480);
var btn = document.getElementById("run");
var cd = document.getElementById("code");
(btn.onclick = function () {
paper.clear();
paper.rect(0, 0, 640, 480, 10).attr({fill: "#fff", stroke: "none"});
try {
(new Function("paper", "window", "document", cd.value)).call(paper, paper);
} catch (e) {
alert(e.message || e);
}
})();
};
This code is from Raphael playground, which mean it implements the raphael library. So the single line of code at the top that I don't understand (it's inside the try/catch expression), suppose to copy the code that user enter that is stored inside cd.value into the function. But how is that possible?
You can visit the page here: http://raphaeljs.com/playground.html
A "noop" or "null function": var noop = function(){}
In python, we can write an empty function or statements by using the 'pass” statement. Writing a pass statement does nothing and is simply used to avoid compile errors while writing an empty function. Above code statements are little different than other popular programming languages like C,C++ or Java.
Do you understand what new Function()
does? It is similar to eval()
in that it takes a string of javascript code - it uses that string to define a function. So the line you posted would be equivalent to doing:
(function(paper,window,document){
/* the code in the cd.value string goes here */
}).call(paper,paper);
More info: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Function
The Function class constructor
functionName = new Function("function code should be written here");
This construct evaluates the code as a string, and is much slower than assigning anonymous functions. It should only be used in places where it is actually needed.
The Function class constructor with parameters
functionName = new Function("varName","varName2","etc.","function code");
It looks like cd.value()
provides a string with javascript code that is going to be parsed and compiled. Later you call it...
You should check how the code in cd.value
looks like.
It's basically creating a new function object with a dynamic body... the best way I can explain it is like this:
function (paper, window, document) where {} = cd.value;
Here's a resource to read more: http://www.permadi.com/tutorial/jsFunc/index.html
The Function
function creates a new function instance with the last parameter as the code of the function.
So it basically does the same as:
eval("function(paper,window,document){"+cd.value+"}").call(paper, paper);
The call
method simply calls the function with the items in the paper
array for arguments.
The arguments to Function
are the named parameters of the function, and then the body of the function. In this case, you have an element whose id is code
and the value
attribute on that element is some Javascript code. Consider that you have this HTML somewhere in your document:
<textarea id="code">
var a = "foo";
var b = "bar";
alert(a+b);
</textarea>
Now, your code example, if run against this code
element, would create the following function:
function(paper, window, document) {
var a = "foo";
var b = "bar";
alert(a+b);
}
You can take a look at the Mozilla Development Center's docs on Function to get a fuller explanation about how the Function
object works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With