JavaScript面向对象重要学问点小结,基于ECMAScript 5.
组织函数
function People(name){
//this能够理解为经由过程new行将建立的对象
this.name = name;
}
//将类实例化
var person = new People('Cassie Xu');
console.log(person.name);
给一个对象给予属性或许要领
function People(name) {
this.name = name;
this.greet = function() {
console.log('Hello, my name is ' + this.name + '!');
};
}
var person = new People('Cassie Xu');
person.greet();
原型(prototype)
why we use prototype? -> 便于要领的重用
与组织函数形式比拟,运用原型对象的优点是能够让一切对象实例同享它所包括的属性和要领。
运行时没找到函数的要领时,对象会起首找它的类的prototype要领
Demo1
function People(name) {
this.name = name;
}
People.prototype = {
greet: function() {
console.log('Hello, my name is ' + this.name + '!');
}
};
var person = new People('Cassie Xu');
person.greet();
//在每一个实例化后的对象都邑有一个__proto__属性,类会将它的prototype赋给实例
//实例化一个对象时,People这个类起首会将person的__proto属性指向People.prototype
console.log(person.__proto__ === People.prototype); // true
Demo2
var a = { foo: 1 };
var b = { bar: 2 };
b.__proto__ = a;
//b本身没有foo属性,然则b会继续寻觅__proto__属性
console.log(b.foo); // 1
console.log(b.bar); // 2
var a = {};
console.log(a.__proto__); // {}
console.log(a.__proto__.__proto__); // null
原型链(prototype chain)
var a = {};
console.log(a.__proto__); // {}
console.log(a.__proto__.__proto__); // null
继续(Inheritance)
Demo1
function Parent(){}
Parent.prototype = {
greet: function(){
console.log('hello world');
}
}
function Child(){}
//此要领适用于父类Child不需要传参数
Child.prototype = new Parent();
var c = new Child();
//c起首寻觅本身的要领,没有great,所以找Child的原型要领,而Child.prototype即是Parent要领
c.greet(); //console.log('hello world');
上面的例子假如Parent有参数,将存在以下题目:
function Parent(a, b) {}
Parent.prototype.greet = function() {
console.log('JavaScript rocks');
}
function Child(a, b) {}
Child.prototype = new Parent(); //something wrong?->new Parent()不能传参数,不然参数一向稳定
var child = new Child();
child.greet();
Demo2
处理父类参数题目
function Parent() {
this.name = 'xxx',
this.date = 'xxx'
}
Parent.prototype.greet = function() {
console.log('JavaScript rocks');
}
function Child() {}
//此要领适用于
Child.prototype = Object.create(Parent.prototype);
//硬记的学问
Child.prototype.constructor = Child;
var child = new Child();
child.greet();
Demo3:继续的一个实例
建立一个矩形
function Rect(width,height){
this._setupDOM(width,height);
}
Rect.prototype._setupDOM = function(width,height){
this._dom = document.createElement('div');
this._dom.style.width = width + 'px';
this._dom.style.height = height + 'px';
};
Rect.prototype.appendToBody = function(){
document.body.appendChild(this._dom);
};
function BlueRect(width,height){
BlueRect._super.apply(this,arguments);
}
BlueRect.prototype = Object.create(Rect.prototype);
BlueRect.prototype.constructor = BlueRect;
BlueRect._super = Rect;
BlueRect.prototype._setupDOM = function(width,height){
BlueRect._super.prototype._setupDOM.apply(this,arguments);
this._dom.style.backgroundColor = 'blue';
};
var br = new BlueRect(200,300);
br.appendToBody();
this关键字
Demo:
function Rect(width,height){
//私有属性加_
this._dom = document.createElement('div');
this._dom.style.width = width + 'px';
this._dom.style.height = height + 'px';
this._dom.style.backgroundColor = 'red';
}
Rect.prototype.appendToBody = function(){
document.body.appendChild(this._dom);
};
var rect = new Rect(100,100);
rect.appendToBody();
修正上面demo:
function Rect(width,height){
this._setupDom(width,height);
}
Rect.prototype._setupDom = function(width,height){
//私有属性加_
this._dom = document.createElement('div');
this._dom.style.width = width + 'px';
this._dom.style.height = height + 'px';
this._dom.style.backgroundColor = 'red';
};
Rect.prototype.appendToBody = function(){
document.body.appendChild(this._dom);
};
var rect = new Rect(100,100);
rect.appendToBody();
var person = {
firstName: "Penelope",
lastName: "Barrymore",
sayFullName: function () {
console.log(this.firstName + " " + this.lastName); //=> "Penelope Barrymore"
console.log(person.firstName + " " + person.lastName); //=> "Penelope Barrymore"
}
};
person.sayFullName();
严厉形式下的this
funtion foo(){
'use strict';
console.log(this) //undefined
}
强迫修正函数上下文的要领
用Object.bind()替代this
function foo(a, b) {
console.log(this);
console.log(a + b);
}
var fooBinding = foo.bind({ name: 'Cassie Xu' });
fooBinding(1, 2);
上面code将输出
[object Object] {
name: "Cassie Xu"
}
运用Object.call/Object.apply实行上下文
call/apply要领都为挪用Object要领,区别是apply将一切参数放到一个数组中去
function foo(a, b) {
console.log(this);
console.log(a + b);
}
foo.call({name:'Cassie Xu'}, 1, 2);
foo.apply({name:'Cassie Xu'}, [1, 2]);
补充其他
自实行函数
why we use it? ->防止泄漏全局变量
(function(c){
var a = 1;
var b = 2;
console.log(a+b+c);
})(3)
// c = 3
闭包作用域
var a = {};
a.foo = function(callback) {
// do something and call callback in whatever way
}
a.bar = function() {
this.num = 1;
var that = this;
//闭包,这里that能够访问到a.bar的作用域
this.foo(function(newNum) {
that.num = newNum;
});
console.log(this.num);
}
a.bar();
本文转自 JavaScript面向对象学问点小结