This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adding a property to the global Object => all objects inherit from this. ex: toString method and prototype. | |
Object.prototype.WTF = "this should not be in your object"; | |
function a() { | |
this.unwanted = 10; | |
this.crap = "dhjbjbfdjbf"; | |
} | |
function child() { | |
this.a = 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( | |
// evaluate the function in the parenthesis | |
function(){} | |
// return the evaluated function object | |
)() // call it immediately | |
// The same functionality can be done as follows; | |
!function(){}() | |
+function(){}() | |
(function(){}()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var foo = 12; | |
function changeFoo() { | |
foo = 34; // changes global scope and not local scope! | |
} | |
changeFoo(); | |
console.log(foo); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function test() { | |
this.arr = [1,2,4]; | |
this.message = "I am here"; | |
this.fun = function() { | |
this.arr.forEach(function(item) { | |
console.log(this.message); // will print undefined 3 times since this refers to the global object | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1 | |
console.log(this); // refers to the Global Object aka window | |
// its methods include prompt alert confirm etc... | |
// 2 | |
function test() { | |
console.log(this); | |
this.method = function inner() {console.log(this)}; | |
}; // Here again "this" will refer to the global object | |
test(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log(typeof "foo" === "string"); //true | |
console.log(typeof String("foo") === "string"); // true | |
console.log(typeof new String("foo") === "string"); // false | |
console.log(typeof 1.2 === "number"); //true | |
console.log(typeof new Date() === "Date"); //false Date is an Object | |
console.log(typeof [1,2,3] === "array"); //false, an array is on object! | |
/** Proper way to test type of object **/ | |
function is(type, obj) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// examples with == | |
1 == "1" // true | |
"/t" == 0 // true | |
"34" == 2 // false | |
new Number(10) == 10 // true | |
Number(10) === 10 //true | |
// examples with === |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Here, the parser will add a semi colon after return causing the function to return undefined. | |
**/ | |
function test(){ | |
var name = "Hello"; | |
return // it will add a ; here | |
{ | |
name: name | |
} | |
} |