Built-in Function object and Array

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
 
P

pcdinh

I am sorry. Please ignore my post. I have discovered my code has a bug

Thanks for reading.
 
T

Thomas 'PointedEars' Lahn

Even though you reported you found the bug in your code, you are probably
not aware of its flaws and inefficiency, and your wrong terminology.
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>

<script type="text/javascript">

See http://validator.w3.org/
// 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);


There exists no valid reason for this error-prone gibberish:

function func()
{
window.alert(this.colors);

var __out = ['<p>Here is a list of '];

// this line in your code does not do anything
this.colors.length;

__out.push(' colors: <ul>');
for (var i = 0, len = this.colors.length; i < len; i++)
{
// probably you did not find this bug:
// ETAGOs must be escaped within CDATA content
__out.push('<li><\/li>');

this.colors = __out.join("");;
}

__out.push('<\/ul><\/p>');

return __out.join("");
}
// Assign the outsite data into internal data
func.colors = colors;

That doesn't make sense with your approach.
// Execute the function
alert(func());

If func() is called this way, the calling object is not `func'. So you are
still accessing the global variable with `this' in the local context, only
as a property of the Global Object. So either you call the function as a
method of itself (`func.call(func)'), or you forego the `func.colors'
assignment, or (recommended) you use `arguments.callee.colors' within the
local context (`var me = arguments.callee'; and `me.colors' is indicated.)
</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).


`this.colors' is not a call, it is a property access. In your case,
write access because of the assignment.
I am struggling with this for several days but can not find the reason
why.


HTH

PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top