Skip to content

Instantly share code, notes, and snippets.

@adohe-zz
Created February 23, 2014 14:00
Show Gist options
  • Select an option

  • Save adohe-zz/9171846 to your computer and use it in GitHub Desktop.

Select an option

Save adohe-zz/9171846 to your computer and use it in GitHub Desktop.
some useful piece of js code
//All this following function expression will
//throw the refrence error, which will raise
//when de-referencing an invalid reference
(function foo() {
console.log('hello world');
})();
//foo();
!function foo() {
console.log('beep boop');
}
//foo();
var bar = function foo() {
console.log('beep boop');
}
//foo();
//Constructor & instanceof
function Beep() {
}
var beep = new Beep();
Boop.prototype = new Beep();
function Boop() {
}
var boop = new Boop();
console.log(beep.constructor === Beep);
console.log(beep instanceof Beep);
console.log(boop.constructor === Beep);
console.log(boop.constructor === Boop);
console.log(boop instanceof Beep);
console.log(boop instanceof Boop);
//Objects private and public members
function MyConstructor(pub, priv) {
var privateVar = priv;
this.publicVar = pub;
this.getPrivateVar = function() {
return privateVar;
}
this.setPrivateVar = function(variable) {
privateVar = variable;
}
this.print = function() {
console.log('public: ' + this.publicVar + ' private: ' + privateVar);
}
}
var o = new MyConstructor(100, 200);
console.log(o.privateVar);
console.log(o.getPrivateVar());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment