前端---JavaScript基础4

本文深入探讨JavaScript中的Call与Apply方法的区别及其使用场景,并详细解析原型链的概念、实现方式及其操作技巧。通过具体实例展示了如何利用Call与Apply进行对象属性继承,以及如何通过原型链实现复杂的数据结构。

前端—JavaScript基础3

在这里插入图片描述

call&apply

//call apply
function Cat(name,color){
	this.name = name;
	this.color = color;
}
var cat = new Cat();
var o = {};
//Cat.call(o,"大白","white");
Cat.apply(o,["大白","white"]);
console.log(o.name);

原型链

function People(){
	this.name = "小王";
	this.sayName = function(){
		console.log(this.name);
	};
}
People.prototype.walk = "walk";
var p1 = new People();
var p2 = new People();
p1.name = "p1";
p2.name = "p2";

在这里插入图片描述

原型链操作

//修改原型
function Animal(){
	this.type= "动物";
}
function Cat(name,color){
	this.name = name;
	this.color = color;
}
Cat.prototype = new Animal();
var a = new Cat("小黑","black");
console.log(a._proto_);  //new Animal()
a._proto_.type = "大动物";
console.log(Cat.prototype); 


//另一种情况
function Animal(){
	this.type= "动物";
}
Animal.prototype.type2 = "动物2";
function Cat(name,color){
	this.name = name;
	this.color = color;
}
Cat.prototype = new Animal();
var a = new Cat("小黑","black");
console.log(a._proto_._proto_);  //
a._proto_._proto_type = "大动物2";
console.log(Cat.prototype._proto_); 


//复杂原型链
function F1(){
	this.name1 = "f1";
}
F1.prototype.name = "first";
function F2(){
	this.name2 = "f2";
}
function F3(){
	this.name3 = "f3";
}
F2.prototype = new F1();
F3.prototype = new F2();
var ff = new F3();
console.log(ff.name2); 
ff._proto_._proto_._proto_.name = "secend";
console.log(F1.prototype); 

delete ff._proto_._proto_._proto_.name
console.log(F1.prototype); 
例子:按钮组件封装:(类似于Bootstrsp里的按钮组件封装)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值