例子
function SuperType(name) {
this.name = name;
this.colors = ["red","green"];
}
SuperType.prototype.sayName = function() {
console.log(this.name);
}
function SubType(name,age) {
SuperType.call(this,name);//继承属性
this.age = age;
}
//继承方法
SubType.prototype = new SuperType();
Subtype.prototype.constructor = Subtype;
Subtype.prototype.sayAge = function() {
console.log(this.age);
}
var instance1 = new SubType("EvanChen",18);
instance1.colors.push("black");
consol.log(instance1.colors);//"red","black"
instance1.sayName();//"EvanChen"
instance1.sayAge();//18
var instance2 = new SubType("EvanChen666",20);
console.log(instance2.colors);//"red","green"
instance2.sayName();//"EvanChen666"
instance2.sayAge();//20
- 思路:创建一个仅用于封装继承过程的函数,该函数在内部以某种方式来增强对象,最后再像真的是它做了所有的工作一样返回对象
例子
function createAnother(original) {
var clone = object(original); //通过调用函数创建一个新对象
clone.sayHi = function () { //以某种方式来增强这个对象
alert("hi");
};
return clone; //返回这个对象
}
var person = {
name:"EvanChen","Van"];
};
var anotherPerson = createAnother(person);
anotherPerson.sayHi();///"hi"
- 思路:通过借用构造函数来继承属性,通过原型链的混成形式来继承方法
-
本质上,就是寄生式继承来继承超类型的原型,然后再将结果指定给子类型的原型基本模型如下所示
function inheritProperty(subType,superType) {
var prototype = object(superType.prototype);//创建对象
prototype.constructor = subType;//增强对象
subType.prototype = prototype;//指定对象
}
例子
function SuperType(name){
this.name = name;
this.colors = ["red","green"];
}
SuperType.prototype.sayName = function (){
alert(this.name);
};
function SubType(name,age){
SuperType.call(this,name);
this.age = age;
}
inheritProperty(SubType,SuperType);
SubType.prototype.sayAge = function() {
alert(this.age);
}
-
原型prototype机制或apply和call方法去实现较简单,建议使用构造函数与原型混合方式
function Parent(){
this.name = 'wang';
}
function Child(){
this.age = 28;
}
Child.prototype = new Parent();//继承了Parent,通过原型
var demo = new Child();
alert(demo.age);
alert(demo.name);//得到被继承的属性
- Object number function boolean undefined
- 强制(parseInt,parseFloat,number)隐式(== ===)
- pop()尾部删除 push()尾部添加
- shift()头部删除 unshift()头部添加
- 执行顺序不一样,
- 参数不一样、
- 事件加不加on、
- this指向问题
- 一个在url后面 一个放在虚拟载体里面
- 有大小限制
- 安全问题
- 应用不同:一个是论坛等只需要请求的,一个是类似需改 密码的
Var ev = ev || window.event
document.documentElement.clientWidth || document.body.clientWidth
Var target = ev.srcElement||ev.target
- 使用eval parse,鉴于安全性考虑,使用parse更靠谱
- 让利用事件冒泡的原理,让自己的所触发的事件,让他的父元素代替执行!
|