您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

JavaScript继承基础强化笔记分享

2024/2/28 8:31:43发布14次查看
出于安全考虑,本地类和宿主类不能继承,其他都可以继承。ecmascript并没有严格的定义抽象类,但是存在一些不允许使用的类。子类将继承超类的所有属性和方法,包括构造函数及方法的实现。记住,所有属性和方法都是公用的,因此子类可直接访问这些方法。子类还可 添加 超类中没有的新属性和方法,也可以 覆盖 超类的属性和方法。
javascript 中的继承机制并不是明确规定的,而是通过 模仿 实现的。这意味着所有的继承细节并非完全由解释程序处理。
对象冒充说白了就是先写一个超类a的构造方法。再写一个类b的构造方法;
然后在b的方法中引用a的构造方法;
使用之后再删除classa的引用。
定义子类属性(所有的子类新属性在删除引用后定义)
// 超类classa的构造方法function classa(scolor) {
this.color = scolor; this.saycolor = function () {、
alert(this.color);
};
}// 子类classb的构造方法function classb(scolor, sname) {
// 引用classa的构造方法
this.newmethod = classa; // 使用classa的构造方法够照classb
this.newmethod(scolor); // 删除对classa的引用
delete this.newmethod; /* 所有的子类新属性在删除引用后定义 */
// 子类属性
this.name = sname; this.sayname = function () {
alert(this.name);
};
}

对象冒充可以实现多重继承
一个类可以继承多个超类。
classx 和 classy,classz 想继承这两个类。
如果存在两个类 classx 和 classy 具有同名的属性或方法,classy 具有高优先级。
因为它从后面的类继承。
function classz() {
// 继承classx
this.newmethod = classx; this.newmethod(); delete this.newmethod; //继承classy
this.newmethod = classy; this.newmethod(); delete this.newmethod;
}

call()方法call() 方法是与经典的对象冒充方法最相似。
一个参数用作 this 的对象。其他参数都直接传递给函数自身。
function classb(scolor, sname) {
/*
将classb付给classa中的this
这时classa中的this实际指向是classb
*/
classa.call(this, scolor); this.name = sname; this.sayname = function () {
alert(this.name);
};
}

apply() 方法两个参数,用作 this 的对象和要传递给函数的参数的数组。
apply第二个参数只能是数组
function classb(scolor, sname) {
// 引用classa构造方法
classa.apply(this, new array(scolor)); // 也可以使用arguments
// 只有超类中的参数顺序与子类中的参数顺序完全一致时才可以传递参数对象
// classa.apply(this, arguments);
// classb 自己的属性
this.name = sname; this.sayname = function () {
alert(this.name);
};
}
原型链(prototype chaining)继承这种形式在 ecmascript 中原本是用于 原型链 的。
// classa的构造方法function classa() {
//要求为空,全部写在prototype上}// classa的属性classa.prototype.color = "blue";
classa.prototype.saycolor = function () {
alert(this.color);
};// classb的构造方法function classb() {}// 继承classa的属性classb.prototype = new classa();// classb自己的属性,需要出现在继承之后classb.prototype.name = "";
classb.prototype.sayname = function () {
alert(this.name);
};
关于 instanceof 运算
在原型链中,instanceof 运算符的运行方式也很独特。对 classb 的所有实例,instanceof 为 classa 和 classb 都返回 true。
var objb = new classb();
alert(objb instanceof classa); //输出 "true"alert(objb instanceof classb); //输出 "true"

混合方式// classa的构造方法,只写属性,不写函数function classa(scolor) {
this.color = scolor;
}// 使用原型给classa赋予函数classa.prototype.saycolor = function () {
alert(this.color);
};// classb的构造方法function classb(scolor, sname) {
// 先调用classa,继承classa的属性
classa.call(this, scolor); this.name = sname;
}// 再通过原型链继承classa的函数classb.prototype = new classa();// 通过原型链定义自己的函数classb.prototype.sayname = function () {
alert(this.name);
};

classb 构造函数中,用对象冒充继承 classa 类的 scolor 属性。
在第二行突出显示的代码中,用原型链继承 classa 类的方法。
javascript基础强化笔记-继承出于安全考虑,本地类和宿主类不能继承,其他都可以继承。
ecmascript并没有严格的定义抽象类,但是存在一些不允许使用的类。
子类将继承超类的所有属性和方法,包括构造函数及方法的实现。记住,所有属性和方法都是公用的,因此子类可直接访问这些方法。子类还可 添加 超类中没有的新属性和方法,也可以 覆盖 超类的属性和方法。
javascript 中的继承机制并不是明确规定的,而是通过 模仿 实现的。这意味着所有的继承细节并非完全由解释程序处理。
对象冒充说白了就是先写一个超类a的构造方法。再写一个类b的构造方法;
然后在b的方法中引用a的构造方法;
使用之后再删除classa的引用。
定义子类属性(所有的子类新属性在删除引用后定义)
// 超类classa的构造方法function classa(scolor) {
this.color = scolor; this.saycolor = function () {、
alert(this.color);
};
}// 子类classb的构造方法function classb(scolor, sname) {
// 引用classa的构造方法
this.newmethod = classa; // 使用classa的构造方法够照classb
this.newmethod(scolor); // 删除对classa的引用
delete this.newmethod; /* 所有的子类新属性在删除引用后定义 */
// 子类属性
this.name = sname; this.sayname = function () {
alert(this.name);
};
}

对象冒充可以实现多重继承
一个类可以继承多个超类。
classx 和 classy,classz 想继承这两个类。
如果存在两个类 classx 和 classy 具有同名的属性或方法,classy 具有高优先级。
因为它从后面的类继承。
function classz() {
// 继承classx
this.newmethod = classx; this.newmethod(); delete this.newmethod; //继承classy
this.newmethod = classy; this.newmethod(); delete this.newmethod;
}

call()方法call() 方法是与经典的对象冒充方法最相似。
一个参数用作 this 的对象。其他参数都直接传递给函数自身。
function classb(scolor, sname) {
/*
将classb付给classa中的this
这时classa中的this实际指向是classb
*/
classa.call(this, scolor); this.name = sname; this.sayname = function () {
alert(this.name);
};
}

apply() 方法两个参数,用作 this 的对象和要传递给函数的参数的数组。
apply第二个参数只能是数组
function classb(scolor, sname) {
// 引用classa构造方法
classa.apply(this, new array(scolor)); // 也可以使用arguments
// 只有超类中的参数顺序与子类中的参数顺序完全一致时才可以传递参数对象
// classa.apply(this, arguments);
// classb 自己的属性
this.name = sname; this.sayname = function () {
alert(this.name);
};
}
原型链(prototype chaining)继承这种形式在 ecmascript 中原本是用于 原型链 的。
// classa的构造方法function classa() {
//要求为空,全部写在prototype上}// classa的属性classa.prototype.color = "blue";
classa.prototype.saycolor = function () {
alert(this.color);
};// classb的构造方法function classb() {}// 继承classa的属性classb.prototype = new classa();// classb自己的属性,需要出现在继承之后classb.prototype.name = "";
classb.prototype.sayname = function () {
alert(this.name);
};
关于 instanceof 运算
在原型链中,instanceof 运算符的运行方式也很独特。对 classb 的所有实例,instanceof 为 classa 和 classb 都返回 true。
var objb = new classb();
alert(objb instanceof classa); //输出 "true"alert(objb instanceof classb); //输出 "true"

混合方式// classa的构造方法,只写属性,不写函数function classa(scolor) {
this.color = scolor;
}// 使用原型给classa赋予函数classa.prototype.saycolor = function () {
alert(this.color);
};// classb的构造方法function classb(scolor, sname) {
// 先调用classa,继承classa的属性
classa.call(this, scolor); this.name = sname;
}// 再通过原型链继承classa的函数classb.prototype = new classa();// 通过原型链定义自己的函数classb.prototype.sayname = function () {
alert(this.name);
};

classb 构造函数中,用对象冒充继承 classa 类的 scolor 属性。
在第二行突出显示的代码中,用原型链继承 classa 类的方法。
相关推荐:
javascript继承体系详解
javascript继承之原型式继承、寄生式继承、寄生组合式继承用法实例详解
javascript继承方式详解
以上就是javascript继承基础强化笔记分享的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product