Skip to content

Instantly share code, notes, and snippets.

@arkitrave
Last active October 9, 2015 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arkitrave/35af1384cd6314601baa to your computer and use it in GitHub Desktop.
Save arkitrave/35af1384cd6314601baa to your computer and use it in GitHub Desktop.
JS Class
foo = 'abc';
console.log(foo.length);
console.log(0.1+0.2);
//////////
var foo = {};
console.log('------ foo');
console.log(foo.__proto__)
console.log(foo.__proto__.__proto__)
function bar () {}
console.log('------ bar')
console.log(bar.__proto__)
console.log(bar.__proto__.__proto__)
console.log(bar.__proto__.__proto__.__proto__)
///////////
//////////
var foo1 = new Function (
'console.log("bar1");'
);
function foo3 () {
console.log('bar3');
}
var foo2 = function () {
console.log('bar2');
};
foo1();
foo2();
foo3();
//////////////////////////////////
function foo () {
var a = 42;
function bar () {
console.log(a);
}
bar();
}
// then
foo();
// then
var orly = foo();
/////////
function foo () {
var a = 42;
function bar () {
console.log(a);
}
return { bar: bar }
}
foo().bar()
////
function foo () {
var a = 42;
function bar () {
console.log(a);
}
return { bar: bar }
}
var orly = foo();
orly.bar(); // a no longer exists! EC still is available though.
orly.bar();
////////////
var outer = function () {
var message="Hello closure!";
this.inner = function () { // this is the global object
console.log(message);
message = "Goodbye closure!";
};
};
outer(); // just sets up inner function
outer = null; // garbage collect!
this.inner(); // but the local var message lives on
this.inner(); // and is mutable
////////////////
var MODULE = (function () {
var
privateVariable = 1,
publicVariable = 2;
function privateMethod () {
// ...
}
function moduleMethod () {
// ...
}
return {
method: moduleMethod,
property: publicVariable
};
}());
///////////////
define('my_module',
[
'dep1',
'dep2'
],
function (dep1, dep2) {
var privateVar = 1;
function init () {
// do stuff
}
function privateMethod () {
// do stuff
}
function publicMethod () {
// do stuff
}
return {
init: init,
method: publicMethod
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment