confirm('Can you read this from where you are sitting?') | titus@theblogfrog.com | |
| @andstuff | |
| Github | https://github.com/tstone |
the original or model on which something is based or formed
var myType = function(){}; var a = new myType();
typeof myType // => "function" typeof a // => "object"
var myType = function(){};
// Members added to the prototype, just like assigning
// keys to a hash
myType.prototype.value = 2000;
myType.prototype['name'] = 'Jim'; var myType = function(){};
myType.prototype.value = 2000;
myType.prototype['name'] = 'Jim';
var x = new myType();
console.log(x.name);
// => "Jim" var x = new myType(); // Didn't happen: x.name = 'Jim';
console.log(x.name);
typeof Object.prototype // => "object"
var typeA = function(){};
var typeB = function(){};
typeB.prototype = new Object();
typeof typeA.prototype === typeof typeB.prototype
// => true // Right: typeB.prototype = new Object(); // Wrong: typeB.prototype = Object;
// ie. This doens't happen in C#
public class typeB : new Object()
{
// ...
} var Being = function(){};
Being.prototype.alive = true;
var Human = function(){};
Human.prototype = new Being();
Human.prototype.constructor = Human; // Ignore for now
var titus = new Human();
titus.alive
// => true // Does Human have a member "alive"? var titus = new Human(); titus.alive // => true
var Invoice = function(){};
Invoice.prototype.amount = 2000;
Invoice.prototype.pay = function(bank) {
bank.transfer(this.amount);
}
var i = new Invoice();
i.pay(...); function debugCurrentUrl() {
// What is the value of "this"?
console.log(this.location.href);
}
debugCurrentUrl();
// => "file:///C:/Users/Titus/Desktop/JS%20Pres/presentation.html" name = 'Bill';
function printName() {
console.log(this.name);
}
var myType = function(){};
myType.prototype.name = 'Jen';
myType.prototype.printName = function() {
console.log(this.name);
}
var a = new myType();
printName();
// => "Bill"
printName.call(a);
a.printName()
// => "Jen" // These are equivalents: printName(); printName.apply(window); // js magic
printName.apply(a);
// These are also equivalents: a.printName() a.prototype.printName.apply(a); // js magic
var parentType = function(){};
parentType.prototype.name = 'Bill';
parentType.prototype.print = function() {
console.log(this.name);
};
var childType = function(){};
childType.prototype.name = 'Frank';
var a = new childType();
a.print();
// => "Frank"; var Widget = function(){};
Widget.prototype.load = function() {
$('button').click(this.onClick);
}
Widget.prototype.onClick = function() {
// this === window!!
} $('button').click(this.onClick()); var Widget = function(){};
Widget.prototype.load = function() {
var that = this;
$('button').click(function() {
that.onClick.apply(that, Array.prototype.slice.apply(arguments));
});
}
Widget.prototype.onClick = function() {
// this === actual instance
} var Widget = function(){};
Widget.prototype.load = function() {
$('button').click(this.onClick.bind(this));
}
Widget.prototype.onClick = function() {
// this === actual instance
} var myType = function(){};
myType.prototype.config = {
vector: 35
};
var a = new myType();
a.config.vector = 25;
var b = new myType();
console.log(b.config.vector);
// => 25;
var myType = function(){};
myType.prototype.init = function() {
this.config = {
vector: 35;
}
};
var a = new myType();
// This is the cost of this pattern:
a.init(); var myType = function(){};
(function(type){
type.init = function() {
// ...
};
type.property = 'value';
}(myType.prototype)); var childType = function(){};
(function(type, parent){
type.init = function() {
// Code that runs before the parent
// ...
// Invoke base method
parent.init.apply(this);
};
}(childType.prototype, parentType.prototype));