var f = function(arg) {
if(arg != null){
document.write(arg);
document.write("<br/>");
}else{document.write("<br/>");}
};
// the keyword this applies to owner of the function
var f1 = function(){
f(this); // window object is this
}();
var data = {
name: "abc",
dowork: function(){
f(this); //will represent the data object
f(this.name); // this can be used to access the data object
}
};
data.dowork();
var f2 = data.dowork.bind(this); // bind lets you change the context in which function will be run
f2(); // because we used bind, f2 will output this = window
//closure and scope
f();
var x = 1;
var f3 = function(){
var y = x; // (closure) this function creates a closure around x which means that x wont get garbage collected so that f3() can be used later when needed.
f(y);
// (scoping) only thing that creates scope in javascript is functions. y cannot be accessed out of this function because a scope has been created around it by function f3.
};
f3();
//namespaces
//namespace is nothing but an empty object
var myns = myns || {};
myns.f4 = function(){
f();f("something");
}();
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.