zartc (13) [Avatar] Offline
#1
hello,

could you add a few sentences clarifying the differences or similarities of defining methods as a) named functions or b) anonymous functions affected to properties.

var myObject = {
function method1() { ... };
method2: function() { ... };
};

Does it make any differences? What is the arguments for and against each form of method definition?

Same questions when defining inner functions:

function outer() {
function method1() { ... };
method2: function() { ... };
}


thanks
bear.bibeault (675) [Avatar] Offline
#2
Re: chapter 3.2 I'd really love some clarification to method declarations
Even better, in the next iteration of the chapter, the ways of declaring functions has been expanded into its own section and should address all of your concerns.
zartc (13) [Avatar] Offline
#3
Re: chapter 3.2 I'd really love some clarification to method declarations
Great
JohnMick (2) [Avatar] Offline
#4
Re: chapter 3.2 I'd really love some clarification to method declarations
Just to point it out...

var myObject = {
function method1() { ... };
method2: function() { ... };
};

Is not valid JavaScript object notation and won't be interpreted - specifically because the function, 'method1' is declared inside of a JavaScript object declaration - which is expecting a parameter name and value delimited with a ':' character.
zartc (13) [Avatar] Offline
#5
Re: chapter 3.2 I'd really love some clarification to method declarations
Thank you for pointing that out.
So, only the property: function() {...} form is valid in both situations. Maybe that explain why the book is using that form so much.
That call for an explanation anyway, because the other form is more natural (for people coming from java)

Thanks
bear.bibeault (675) [Avatar] Offline
#6
Re: chapter 3.2 I'd really love some clarification to method declarations
Yes, again I think you will very pleased with the new chapter when it is released in the next MEAP round.

Using JavaScript as a functional language is an important leap for Java developers, so the chapter has been greatly enhanced to discuss how functions are used in JavaScript.
JohnMick (2) [Avatar] Offline
#7
Re: chapter 3.2 I'd really love some clarification to method declarations
It sounds like the new chapter is going to be a nice improvement to the current draft of the book.

To continue the discussion a bit here: method1 would be perfectly valid if you didn't specify it inside of the object notation block.

function method1() {

}

var someObject = {
method2: = function() {}
};

But the differences in declaration allow for different ways to invoke these two functions.

Assuming we are inside of the same scope - you can only call method1 in a fashion similar to Java: method1()

However method2 can be called a couple of different ways:
approach1: someObject.method2();
or
approach2: someObject["method2"]()

By leveraging JavaScript's object parameter notation used in approach2 you are able to develop a framework where depending on some input - different functions may be called. Here is the basic idea - though I would recommend waiting for the next draft of the chapter before finalizing these type of an approach for yourself; the authors will certainly have other suggestions and points to keep in mind when designing something like this:

var objectContainingParametersWhichAreFunctions = {
func1: function() { ... },
func2: function() { ... },
func3: function() { ... }
};

function someFunctionCalledWhenSomeActionOccurs(functionName) {
// functionName is a string which would be either 'func1', 'func2', or 'func3'
objectContainingParametersWhichAreFunctions[functionName]();
}
zartc (13) [Avatar] Offline
#8
Re: chapter 3.2 I'd really love some clarification to method declarations
whoa, apparently there is much to discover about javascript function (declaration forms, usage patterns, etc).

I duno if what JohnMick wrote in its previous post is in the book (I've only read 1/4 of the book so far) but sure it could find its place there.

I can't wait for the next MEAP...

Thank you all for sharing your knowledge.
bear.bibeault (675) [Avatar] Offline
#9
Re: chapter 3.2 I'd really love some clarification to method declarations
Yes, the book now goes into copious detail on the various ways that functions can be declared, and even more importantly, on the various way that they can be invoked.

The latter point is vitally important, especially for people coming from Java, because the behavior of a function is much more determined by how it is invoked rather than by how it is declared!