call an anonymous function as a method?

J

jrough

I am trying to understand how to invoke a method of a function. Since
this is an anonymous function can you call it like this?

myString='This is a string';

Var myObject= {

myFunction:function(arg1){
Var temp= new array();
temp = split(arg1);
for (var i in temp) {
document.writeline.temp;
}
}
}

myObject.myFunction();
 
A

Asen Bozhilov

jrough said:
I am trying to understand how to invoke a method of a function.  Since
this is an anonymous function can you call it like this?

In Javascript (ECMAScript) functions are first-class objects. This
means that they are created runtime, during execution of the program.
Therefore they can be assigned as certain data structures, such as
variables, properties and they can be passed as arguments to other
functions.

Your program would make sense if it was not full of syntax errors.
myString='This is a string';

Undeclared assignment!
Var myObject= {

ECMAScript is case sensitive language. So `Var` is not a keyword
`var`. During parsing stage of your program, the parser treats `Var'
as an Identifier instead of keyword. Two Identifiers separated by
white space are not valid sequence for parser and therefore it throws
a SyntaxError.

myFunction:function(arg1){
Var temp= new array();

`array' != `Array'
temp = split(arg1);
for (var i in temp) {
  document.writeline.temp;
        }
    }

}

^
Missing semicolon. Expression statements as AssignmentExpression
should be terminated with `;` semicolon. In particular case it is not
a SyntaxError, but it is good habit if you put semicolon in explicit
way.
myObject.myFunction();

Yes, it is valid CallExpression, because you have assigned a value of
`myFunction' property which refer created function object by
evaluating of FunctionExpression. The created object has internal
[[Call]] method and therefore it can be used in CallExpression.
 
E

Evertjan.

Asen Bozhilov wrote on 24 feb 2011 in comp.lang.javascript:
In Javascript (ECMAScript) functions are first-class objects. This
means that they are created runtime, during execution of the program.
Therefore they can be assigned as certain data structures, such as
variables, properties and they can be passed as arguments to other
functions.

Your program would make sense if it was not full of syntax errors.


Undeclared assignment!


ECMAScript is case sensitive language. So `Var` is not a keyword
`var`. During parsing stage of your program, the parser treats `Var'
as an Identifier instead of keyword. Two Identifiers separated by
white space are not valid sequence for parser and therefore it throws
a SyntaxError.



`array' != `Array'

Nothing is split here, try:

var temp = aString.split(arg1)

for (var i in temp) {
  document.writeline.temp;


writeline.something is not Javascript, try:

document.write(temp + '<br>');


        }
    }

}
^
Missing semicolon. Expression statements as AssignmentExpression
should be terminated with `;` semicolon. In particular case it is not
a SyntaxError, but it is good habit if you put semicolon in explicit
way.
myObject.myFunction();

Yes, it is valid CallExpression, because you have assigned a value of
`myFunction' property which refer created function object by
evaluating of FunctionExpression. The created object has internal
[[Call]] method and therefore it can be used in CallExpression.
 
R

RobG

In Javascript (ECMAScript) functions are first-class objects. This
means that they are created runtime, during execution of the program.
Therefore they can be assigned as certain data structures, such as
variables, properties and they can be passed as arguments to other
functions.

Your program would make sense if it was not full of syntax errors.

You missed (at least?) one - easy to do when there are so many. :)
Undeclared assignment!


ECMAScript is case sensitive language. So `Var` is not a keyword
`var`. During parsing stage of your program, the parser treats `Var'
as an Identifier instead of keyword. Two Identifiers separated by
white space are not valid sequence for parser and therefore it throws
a SyntaxError.



`array' != `Array'

Error: split is undefined. Perhaps the OP meant:

temp = arg1.split();

which means that temp == arg1 as the separator is not defined. Maybe:

temp = arg1.split(' ');

to get an array of the words in myString?
for (var i in temp) {
  document.writeline.temp;
        }
    }


   ^
Missing semicolon. Expression statements as AssignmentExpression
should be terminated with `;` semicolon. In particular case it is not
a SyntaxError, but it is good habit if you put semicolon in explicit
way.
myObject.myFunction();


Perhaps this was meant to be:

myObject.myFunction(myString);
 
T

Thomas 'PointedEars' Lahn

RobG said:
Error: split is undefined. Perhaps the OP meant:

temp = arg1.split();

which means that temp == arg1 as the separator is not defined.

No, it means temp[0] === arg1, temp.length === 1. String.prototype.split()
always returns a reference to an Array that contains one or more string
values.
Maybe:

temp = arg1.split(' ');

to get an array of the words in myString?

Better:

temp = arg1.split(/\W+/);

or similar (note that "words" are defined as "'Latin' characters or
underscore"). "x y".split(' ') will return ["x", "", "y"], although
there are arguably only two words.


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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top