Write once, run anywhere
var i = 1;
var handler;
handler = function(){console.log(i)};
//i=2;
handler();
var ids = list
.filter(if("active"))
.map(field("id"))
.toArray();
var sum = plus(4)(5);
Forget about functions: class, method, constructor
Class or constructor function
function Student(name, grade) {
this.name = name;
this.grade = grade;
}
var s = new Student("Вася Пупкин", 5);
s.constructor == Student;
s instanceof Student;
function Student(name, grade) {
var self = this;
this.name = name;
this.grade = grade;
this.sayHello = function() {
console.log(getDisplayName());
};
function getDisplayName() {
return self.name + ": " + self.grade;
}
}
function Student(name, grade) {
this.name = name;
this.grade = grade;
}
Student.prototype.sayHello = function() {
console.log(this._getDisplayName());
};
Student.prototype._getDisplayName = function() {
return this.name + ": " + this.grade;
};
function Student(name, grade) { this.name = name; this.grade = grade; }
Student.settings = {
nameCharLimit: 100
};
Student.getAllStudents = function (){
return [];
};
console.log(Student.settings.nameCharLimit);
console.log(Student.getAllStudents());
function DummyStudent(name) { Student.apply(this, [name, 2]); this.idle = function () { console.log("Idling..."); }; }
var d = new DummyStudent("Виктор Я...ч"); if(d.idle) { // Duck typing d.idle(); }
function DummyStudent(name) { this.name = name; this.grade = 2; } DummyStudent.prototype = new Student(); DummyStudent.prototype.idle = function () { console.log("Idling..."); };
var d = new DummyStudent("Виктор Я...ч"); if(d instanceof DummyStudent) { d.idle(); }
function GossipyStudent(name) {
Student.apply(this, [name, 2]);
var base = {
sayHello : this.sayHello
};
this.sayHello = function () {
console.log("Bla bla bla ...");
base.sayHello();
};
}
var d = new GossipyStudent("Виктор X...o");
d.sayHello();
function GossipyStudent(name) {
this.name = name;
this.grade = 2;
}
GossipyStudent.prototype = new Student();
GossipyStudent.prototype.sayHello = function () {
console.log("Bla bla bla ...");
Student.prototype.sayHello.call(this);
};
var d = new GossipyStudent("Виктор X...o");
d.sayHello();
var game = new HeadsOrTailsGame();
var name = game.name; // Орел-решка
var result = game.play(); // 1 or 0
result = game.getPreviousResult(); // 1 or 0
var m = new Monster(2);
m.goLeft();
m.goDown();
var d = m.totalDistance();
var angle = m.direction(); // Direction angle