面向对象
创建对象
function Student1(name) {
this.name = name;
this.hello = function () {
console.log('Hello, ' + this.name + '!');
}
}
var xiaoming = new Student1('小明');
xiaoming.name;
xiaoming.hello();
function Student2(name) {
this.name = name;
}
Student2.prototype.hello = function () {
alert('Hello, ' + this.name + '!');
};
var xiaohong = new Student2('小红');
var xiaowang = new Student2('小王');
xiaohong.hello === xiaowang.hello;
function Student3(props) {
this.name = props.name || '匿名';
this.grade = props.grade || 1;
}
Student3.prototype.hello = function () {
alert('Hello, ' + this.name + '!');
};
function createStudent3(props) {
return new Student3(props || {})
}
var xiaojun = createStudent3({
name: '小军'
});
xiaojun.grade;
原型继承
function Student(props) {
this.name = props.name || 'Unnamed';
}
Student.prototype.hello = function () {
alert('Hello, ' + this.name + '!');
}
function PrimaryStudent(props) {
Student.call(this, props);
this.grade = props.grade || 1;
}
function inherits(Child, Parent) {
var F = function () {};
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
}
inherits(PrimaryStudent, Student);
PrimaryStudent.prototype.getGrade = function () {
return this.grade;
};
var xiaoming = new PrimaryStudent({
name: '小明',
grade: 2
});
xiaoming.name;
xiaoming.grade;
xiaoming.__proto__ === PrimaryStudent.prototype;
xiaoming.__proto__.__proto__ === Student.prototype;
xiaoming instanceof PrimaryStudent;
xiaoming instanceof Student;
class继承
class Student {
constructor(name) {
this.name = name;
}
hello() {
console.log('Hello, ' + this.name + '!');
}
}
var xiaoming = new Student('小明');
xiaoming.hello();
class PrimaryStudent extends Student {
constructor(name, grade) {
super(name);
this.grade = grade;
}
myGrade() {
console.log('I am at grade ' + this.grade);
}
}
var xiaohong = new PrimaryStudent('小红', 2);
xiaohong.hello();
xiaohong.myGrade();
promise
function test(resolve, reject) {
var timeout = Math.random() * 2;
console.log('set timeout to: ' + timeout + ' seconds.');
setTimeout(function () {
if (timeout < 1) {
console.log('timeout < 1 call resolve()...');
resolve('200 OK');
} else {
console.log('timeout >= 1 call reject()...');
reject('timeout in ' + timeout + ' seconds.');
}
}, timeout * 1000);
}
new Promise(test).then(function (result) {
console.log('成功:' + result);
}).catch(function (reason) {
console.log('失败:' + reason);
});
function multiply(input) {
return new Promise(function (resolve, reject) {
console.log('calculating ' + input + ' x ' + input + '...');
setTimeout(resolve, 500, input * input);
});
}
function add(input) {
return new Promise(function (resolve, reject) {
console.log('calculating ' + input + ' + ' + input + '...');
setTimeout(resolve, 500, input + input);
});
}
var p = new Promise(function (resolve, reject) {
setTimeout(function () {
console.log('start serial Promise...');
resolve(123);
}, 2500);
});
p.then(multiply)
.then(add)
.then(multiply)
.then(add)
.then(function (result) {
console.log('Got value: ' + result);
});
var p1 = new Promise(function (resolve, reject) {
setTimeout(function () {
console.log('Start parallel Promise: P1');
resolve('P1');
}, 5500);
});
var p2 = new Promise(function (resolve, reject) {
setTimeout(function () {
console.log('Start parallel Promise: P2');
resolve('P2');
}, 5600);
});
Promise.all([p1, p2]).then(function (results) {
console.log('Get P1 P2 result: ' + results);
});
var p1 = new Promise(function (resolve, reject) {
setTimeout(resolve, 6500, 'P1');
});
var p2 = new Promise(function (resolve, reject) {
setTimeout(resolve, 6600, 'P2');
});
Promise.race([p1, p2]).then(function (result) {
console.log(result);
});