Menu
Hi there,
Sorry for the dumb question, I’ve been googling all over and I can’t find an answer to ths.
I notice that if I do this:
[CODE]
var fnc = function() {
var a = 10;
fnc.b = 10;
}
fnc.c = 4
alert(fnc.a);
alert(fnc.b);
alert(fnc.c);
the first two alerts show undefined and the third shows 4. Presumably c is defined as an attribute of fnc. My question is, why are a and b not attributes of fnc.
Sorry again for the dumb question ๐ฎ
[CODE]
var fnc = function() {
}
fnc.c = 4
alert(fnc)
alert(fnc.c);
[/CODE]
[CODE]
var fnc = function fnc() {
fnc.c = 5;
}
fnc();
alert(fnc)
alert(fnc.c);
[/CODE]
<i>
</i>var fnc = {
a:10,
b:10
}
fnc.c = 4;
<i>
</i>var Constructor = function(a,b,c){
this.a=a;
this.b=b;
this.c=c;
}
fnc = new Constructor(10,10,4);
<i>
</i>var fnc = function(){
}
<i>
</i>var fnc = function(){
alert(arguments.callee.c);
}
fnc.c=4;
fnc();
[CODE]var str = "Hello";
str.a = "Hi There";
alert(str.a);
// => undefined[/CODE]
[CODE]var fnc = function() {}
fnc.a = "Hi There";
alert(fnc.a);
// => "Hi There"[/CODE]
<i>
</i>var str = new String("Hello")
str.a="Hi There"
alert(str.a)
<i>
</i>var str="Hello"; [COLOR="Blue"]// this is an expression, the result is a primitive String[/COLOR]
var str = new String("Hello"); [COLOR="Blue"]// this is a construction, the result is an object String[/COLOR]
<i>
</i>String.prototype.a="Hi There";
var str = "Hello";
alert(str.a);
Anyway - you can give a function literal members (though I'm not entirely sure why you would). The only reason you haven't been having success, is that you haven't actually called the function - you've only set it. if you call add 'fnc();' after you declare the literal, any member declarations within it will be processed.[/QUOTE]
<i>
</i>var fnc=function(){
fnc.a=10
}
fnc();
alert(fnc.a)
<i>
</i>var fnc=function(){
}
fnc.a=10
alert(fnc.a)
[CODE]alert("Hello".charAt(2));
=> l[/CODE]
Hi goldfidget,
I'm not really sure why you're trying to do what your doing, but I think the reason you are not being able to set fnc.c was returning null, is that you have only ever defined the function literal and not actually called it.
If you call it, it will set correctly, and be useable outside the function e.g.
[CODE]
var fnc = function fnc() {
fnc.c = 5;
}
fnc();
alert(fnc)
alert(fnc.c);
[/CODE]
Does this help?
Cheers,
Skinicod[/QUOTE]
The primitive still has methods though
[/quote]
More about functions (and Function) scope:
https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope [/QUOTE]
Despite primitives, [I]functions are objects[/I]. But their content is not a member of that object. Their content is a piece of code, a sub-routine which has to be run whenever the function is called.[/QUOTE]
So, given the above, is there a way for a function to refer to itself outside of a constructor without necessarily knowing its own name?[/QUOTE]
<i>
</i>function fnc(){
var thisFunction=[COLOR="Blue"]arguments.callee[/COLOR];
alert(thisFunction)
}
fnc();
There is a small confusion about the fact that [B]arguments.callee[/B] is or not deprecated. Well, Mozilla stipulates clearly, I think:
"JavaScript 1.4: deprecated arguments, arguments.callee, and arguments.length [I]as properties of Function instances[/I];[COLOR="Blue"][B]retained[/B] arguments as a local variable of a function and arguments.callee and arguments.length as properties of this variable[/COLOR] .[/QUOTE]
[CODE]
function one(){
return two();
}
function two(){
return "two says: " + arguments.callee.caller.toString();
}
alert( one() ) [/CODE]
0.1.9 โ BETA 5.28