Small prototype tests
by Nitrium
JavaScript
Object.prototype.setCount = function(_count) {
this.count = _count;
}
Object.prototype.getCount = function() {
return this.count;
}
var Zoo = {};
Zoo.setCount(5);
$('html').append(Zoo.getCount());
// -----------------------------------------------
var Animal = function(_type) {
this.type = _type;
}
Animal.prototype.getType = function() {
return this.type;
}
var Cheetah = new Animal('Jake');
try {
$('html').append('type: ' + Cheetah.getType());
} catch (e) {
$('html').append(e);
}
Animal.prototype.talkType = function(_str) {
return ('hello, ' + _str + ' ' + this.type);
}
$('html').append(Cheetah.talkType('my name is'));
// -----------------------------------------------
var localTest = function() {
var localVar = 'test';
globalVar = 'testG';
}
localTest();
$('html').append(' tests: ' + globalVar);
// -----------------------------------------------
var nullVar = null;
var undefVar;
var bothVar;
if (nullVar === null) {
$('html').append('<b>' + 'it\'s null' + '</b>');
}
if (undefVar === undefined) {
$('html').append('<b>' + 'it\'s undefined' + '</b>');
}
if (bothVar == null) {
$('html').append('<b>' + 'it\'s both' + '</b>');
}
// -----------------------------------------------
var Manager = function() {
this.name = 'John';
this.department = 'general';
}
var Employee = function() {
this.surname = 'Drake';
this.projects = [];
}
Employee.prototype = new Manager;
var jim = new Employee;
$('html').append(jim.name, '<br>');
// ------------------------------------------------
var animals = [
{species: 'Lion', name: 'King'},
{species: 'Whale', name: 'Fail'}
];
for (var i = 0; i < animals.length; i++) {
(function (i) {
$('html').append('#' + i + ' ' + this.species + ': ' + this.name);
}).call(animals[i], i);
}