Javascript new way to define object properties and functions? [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?
I have stumbled upon many times this unusual way of coding, and it seems to be becoming popular.
(function (variable) {
// properties or methods go here
// some more stuff
})(variable)
It's hard for me to even research it, because i don't even know how it's called. i have worked with it with jquery with but i still don't know how it
works.
example:
(function ($) {
...
// code to manipulate the dom
function init() {
.....
}
$(document).ready(function () {
init();
});
})(jQuery);
I've only used it because i was updating some code made by another developer.
Is there any advantage to coding like this? Is there a place where i can read more about it?
If anybody understand my question, it will be nice to see some articles that talk about this, or maybe you have some insight on how to make your own.
Thank You
Ibu

Its called a self invoking anonymous function. Look at this thread for more details about how it works and why its used,
Why do you need to invoke an anonymous function on the same line?

Related

d3 examples, is chart = { ... } a function or class or object? [duplicate]

This question already has an answer here:
Return value in non-function Javascript code block
(1 answer)
Closed 1 year ago.
In all the d3 examples like this one , the svg appending part of the code is encapsulated in
chart = {
... lines and lines of codes ...
}
I first thought it is a function, since there's a return statement in it. But of all the arrow function I read up in MDN seemed all require an arrow =>.
Is this a object? But object are variables and functions with colons, but this one do not.
I'm confused as to what it is. Could you help me to understand?
The best way to understand the Observable's samples and work with them, is to download the code and run it in a fiddle, sandbox, or any other environment.

What's the purpose of closures in MVVM modules? [duplicate]

This question already has answers here:
What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?
(10 answers)
Closed 8 years ago.
I'm working with the following:
define(["knockout"], function(ko) {
var vm = this;
(function() { // I'm tempted to delete this
// init
vm.data = ko.observable("");
// other stuff
})(); // and this
return vm;
});
The person who wrote this said they thought it was a best practice but didn't know why. I understand this is a closure but we don't need any of the "private" functionality that closures provide in this instance, so this just seems like noise to me, but I'm probably overlooking something. What's the point?
You will find a full explanation on that notation in answers to this question: What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?
Short version (sample of accepted answer):
What you're doing when you write (function() { ... code ... })(), is
you're making the code inside a function literal (meaning the whole
"object" is actually a function). After that, you're self-invoking the
function (the final ()). So the major advantage of this as I mentioned
before, is that you can have private methods/functions and properties:
> (function() { var private_var;
>
> function private_function() {
> //code } })()

why do we need to pass in window and undefined into this jquery plugin? [duplicate]

This question already has answers here:
What is the purpose of passing-in undefined?
(3 answers)
Closed 8 years ago.
I'm looking at jquery resize plugin and can't understand certain things about how it works:
usually we only pass in Jquery object into jquery plugins, like this:
(function($){
....plugin code....
})(jQuery);
In "resize" plugin there are window and undefined objects being passed in:
(function($,window,undefined){
....plugin code....
})(jQuery,this);
IMHO - window is a global object anyway - why do we need to pass it in? the logic behind passing in undefined object I understand even less. I'm sure there's gotta be some reason for it - but I cannot think of any.
Can someone explain why is it being done?
this is explained very well in this video.
basically, you can set those variables in the self invoking function to ensure they work as expected.
"the asshole effect" undefined = true; -paul irish
furthermore by passing these as arguments they can also be minified.
ie.
(function(A,B,C){
....plugin code....
})(jQuery,this);

JavaScript - Get calling object [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Javascript how do you find the caller function?
Is there a way to get the value of this from the function which has called the current function?
Look at this:
function TraceMySelf(){
console.log(this);
}
function A(){
TraceMySelf();
console.log(this);
}
var a = new A();
When this code is executed, the console displays first the window object and then the a object. How can I make the code display the a object twice, with only changing line 2? I know that I could apply the function inside A with this, but that isn't what I want.
Is this possible?
I think this is the answer to your question: StackOverflow 280389
However, I think the right answer is "don't do that". I think it runs counter to how JavaScript is designed.
It might also be worth looking at jQuery Proxy for another way of linking function and object.

jQuery's function($) (jQuery) syntax [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
jQuery: What does (function($) {})(jQuery); mean?
I stumbled up on the following code (included in one file) but I just cannot understand what it really means.
(function ($) {
function doSomething1(somedata) {
}
function doSomething1(somedata) {
}
})(jQuery);
Question 1:
What does this syntax mean in the contex of jQuery
Question 2:
How can we call these functions let say from other files such as the HTML index file and other JavaScript files?
Thanks
This syntax is not particularly special to jquery, it's normal javascript. Here simply function
function ($) {
// some code here...
}
(note that it takes argument named $) is called with parameter jQuery (which is, obviously, global object of jQuery framework).
This is usually done when there're several js frameworks on one page (jquery, dojo, prototype, etc) which all redefine global variable $. But with this code, inside doSomething1 or doSomething2 you can always call $('.test') and be certain that call will be processed by jquery, not dojo. Because $ is not a global variable in this case, it's function parameter.
I'm not sure about your question here, but the (function() means it's self-executing,
and you call them by importing the files in the main page and then calling
doSomething1()
Mostly likely that was jQuery plugin: Plugins/Authoring

Categories