I am learning Javascript, I have been using PHP for about 10 years so I have some knowledge of Javascript, mostly just using jQuery and hacking it together, I think it's time I put some effort into learning it better so I have been reading up on it.
Below are my examples of defining and calling some functions.
Method 1
function testFunction1() {
console.log('TestFunction1() was ran');
}
testFunction1();
Method 2
var testFunction2 = function() {
console.log('TestFunction2() was ran');
}
testFunction2();
Method 3
var TestFunction3 = {
flag: function() {
console.log('TestFunction3.flag() was ran');
},
unflag: function() {
console.log('TestFunction3.unflag() was ran');
}
};
TestFunction3.flag();
TestFunction3.unflag();
Method 4
var TestFunction4 = {
Like: {
comment: function() {
console.log('TestFunction4.Like.comment() was ran');
},
user: function() {
console.log('TestFunction4.Like.user() was ran');
}
},
Unlike: {
comment: function() {
console.log('TestFunction4.Unlike.comment() was ran');
},
user: function() {
console.log('TestFunction4.Unlike.user() was ran');
}
}
};
TestFunction4.Like.comment();
TestFunction4.Like.user();
TestFunction4.Unlike.comment();
TestFunction4.Unlike.user();
Ok so I understand method 1 and 2 to be just a basic function call.
1)
Method 3 and 4 are where my questions start, from other post and from reading, I cannot tell if these are still considered a basic function with namespacing applied, or if these would be considered Objects?
2)
I have seen where sometimes an object would be called with the new word however running all this in the browser works fine so I am guessing this is not an object? If it is not an object, how would I make it into an object?
3)
Example 3 and 4 are pretty much the same with the exception that example 4 has functions defined 1 level deeper then example 3, is there a name for example 3 and 4 or are they considered the same thing?
4)
Lastly of all the 4 examples, is any of these 4 methods preferred over the other?
Sorry for all the questions in 1 but they are all related and I don't think I need to start 4 separate questions for this.
nullandundefined.nullis an object.