Skip to content

Instantly share code, notes, and snippets.

@snippe
snippe / context_man.js
Created November 9, 2014 01:14
Context Manipulation with .apply and .call
var obj1 = {
num : 4
}
function multiply(val){
return this.num * val;
}
console.log(multiply(3)); //impossible because no num defined in window (NaN)
console.log(multiply.apply(obj1, [4])); //16
@snippe
snippe / context_man2.html
Created November 9, 2014 01:20
Specifying object
<button id='thebutton'>Click me!</button>
@snippe
snippe / bind_js1.js
Created November 9, 2014 01:27
.bind in JavaScript
this.statusId = 1;
var module = {
statusId : 4,
getStatus : function(){
return this.statusId;
}
};
console.log(module.getStatus()); //4
var first_object = {
num: 4
};
function multi(mul) {
return this.num * mul;
}
Function.prototype.bind = function (obj) {
var method = this,
var first_object = {
a: 2,
b: 4
};
function adder(val1, val2) {
return this.a + this.b + val1 + val2;
}
Function.prototype.bind = function (obj) {
console.log("Before " + value1); //Before undefined
var value1 = "This is value1";
console.log("After " + value1); //After This is value1
var var1 = 1;
var var2 = 2;
(function () {
console.log("var1 = " + var1);
var var1 = 10;
console.log("var2 = " + var2);
console.log("var1 =" + var1);
class X
{
protected virtual void F() { Console.WriteLine("X.F"); }
protected virtual void F2() { Console.WriteLine("X.F2"); }
}
class Y : X
{
sealed protected override void F() { Console.WriteLine("Y.F"); }
protected override void F2() { Console.WriteLine("Y.F2"); }
}
var isAsync = true;
function callWebService(){
console.log('Calling web service');
$.ajax({
url: "http://date.jsontest.com/",
async : isAsync
}).done(function (data) {
console.log(data);
});
$.when($.ajax('http://date.jsontest.com'))
.then(function(data){
//Will be executed if the ajax is successful
console.log('data ' + data);
}).
fail(function(data){
//Will be executed if the ajax gets fails
console.log('Fail');
});