Jump to content

function ordering related to objets


jimfog

Recommended Posts

My question is the following:Does it have any importance the ordering of the code when we are talking about a function definition and subsequently itsallocation to an object as a method?I mean, is there a difference between defining the function FIRST, then allocating it to an object as method in comparison with defining the object FIRST and then writing the function second?Does the ordering of the code plays a role in javascript when we are dealing with functions and objects?

Link to comment
Share on other sites

should be pretty easy to test, no?

var Logger = function(text){  console.log(text);};var myObj = {  log : Logger};myObj.log("test text");

vs.

var myObj = {  log : Logger};var Logger = function(text){  console.log(text);};myObj.log("test text");

vs.

var myObj = {  log : Logger};function Logger(text){  console.log(text);};myObj.log("test text");

edit: added JSG 'test'right?

Link to comment
Share on other sites

It depends on the syntax you use. In the syntax above it matters because the function is being declared as a variable. If you define the function like this then it doesn't matter where you define it:

function test() { ... }

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...