P
pcdinh
I tried to make a javascript function based on Function object and
assign it an outside data, expecting that that function will return a
string with data populated.
<script>
// Data
var colors = ['Red', 'Green', 'Blue', 'Orange']
// Function body with some variables
var functionBody = "alert(this.colors); var __out = '<p>Here is a list
of '; this.colors.length; __out += ' colors: <ul> '; for (var i=0;
i<this.colors.length; i++) { __out += ' <li>'; this.colors = __out
+= '</li> '; } __out += ' </ul></p>'; return __out;";
// Assign the function body to function named func
var func = new Function(functionBody);
// Assign the outsite data into internal data
func.colors = colors;
// Execute the function
alert(func());
</script>
When the script is executed, alert(this.colors) call in function body
will return an dialog to notify the content of the array assigned
before. It works as usual there but the call this.colors returns
nothing (undefined).
I am struggling with this for several days but can not find the reason
why.
Could you help me this?
Thanks a million
Dinh
assign it an outside data, expecting that that function will return a
string with data populated.
<script>
// Data
var colors = ['Red', 'Green', 'Blue', 'Orange']
// Function body with some variables
var functionBody = "alert(this.colors); var __out = '<p>Here is a list
of '; this.colors.length; __out += ' colors: <ul> '; for (var i=0;
i<this.colors.length; i++) { __out += ' <li>'; this.colors = __out
+= '</li> '; } __out += ' </ul></p>'; return __out;";
// Assign the function body to function named func
var func = new Function(functionBody);
// Assign the outsite data into internal data
func.colors = colors;
// Execute the function
alert(func());
</script>
When the script is executed, alert(this.colors) call in function body
will return an dialog to notify the content of the array assigned
before. It works as usual there but the call this.colors returns
nothing (undefined).
I am struggling with this for several days but can not find the reason
why.
Could you help me this?
Thanks a million
Dinh