在这之前先说一下原型的基本知识。
什么是原型?简单来说就是一个对象内部关联另外一个对象,本质来说就是对象与对象之间的关联;
一个对象本身没有属性或者方法会到原型对象上查找。
这里每个例子会通过构造函数,class和行为委托来不同实现,不过不会评论class,是否使用class取决于你的观点。
先看一个例子,构造函数写法
function Foo(name) {
this.name = name;
}
Foo.prototype.age = function() {
console.log(this.name);
};
function Fn(name) {
Foo.call(this);
this.name = name;
}
Fn.prototype = Object.create(Foo.prototype);
Fn.prototype.constructor = Fn.prototype;
Fn.prototype.set = function(value) {
this.name = value;
};
var a1 = new Fn('zhangsan');
a1.age(); //zhangsan
var a2 = new Fn('lisi');
a2.set('xiaowu');
a2.age(); //xiaowu
class写法
class Foo {
constructor(name) {
this.name = name;
}
age() {
console.log(this.name);
}
}
class Fn extends Foo {
constructor(name) {
super();
this.name = name;
}
set(value) {
this.name = value;
}
}
var a1 = new Fn('zhangsan');
a1.age(); //zhangsan
var a2 = new Fn('lisi');
a2.set('xiaowu');
a2.age(); //xiaowu
行为委托写法
var Foo = { const(name) { this.name = name; }, age() { console.log(this.name); } }; var Fn = Object.create(Foo); Fn.set = function (value) { this.name = value; }; var a1 = Object.create(Fn); a1.const('zhangsan'); a1.age(); //zhangsan var a2 = Object.create(Fn); a2.set('xiaowu'); a2.age(); //xiaowu
可以看到比起构造函数,行为委托是通过原型链来实现的,他值关心一件事情,对象指向的关系。
再来看一个复杂一些的例子,为dom添加样式。
function Foo(value) {
this.dom = value;
}
Foo.prototype.div = function () {
var div = document.createElement(this.dom);
this._dom = document.body.appendChild(div);
return this._dom;
};
Foo.prototype.get = function () {
return this._dom;
};
function Fn(text, cssText) {
Foo.call(this, 'div');
this.text = text;
this.cssText = cssText;
}
Fn.prototype = Object.create(Foo.prototype);
Fn.prototype.constructor = Fn.prototype;
Fn.prototype.set = function () {
this.div();
var div = this.get();
div.textContent = this.text;
Object.keys(this.cssText).forEach((name) => {
div.style.cssText += `${name}: ${this.cssText[name]}`;
});
};
var a = new Fn('hi', {color: 'red', "font-size": '16px'});
a.set();
class写法
class Foo {
constructor(value) {
this.dom = value;
}
div() {
var div = document.createElement(this.dom);
this._dom = document.body.appendChild(div);
return this._dom;
}
get() {
return this._dom;
}
}
class Fn extends Foo {
constructor(text, cssText) {
super('div');
this.text = text;
this.cssText = cssText;
}
set() {
super.div();
var div = super.get();
div.textContent = this.text;
Object.keys(this.cssText).forEach((name) => {
div.style.cssText += `${name}: ${this.cssText[name]}`;
});
}
}
var a = new Fn('hi', { color: 'red', "font-size": '16px' });
a.set();
行为委托写法
var Foo = {
const(value) {
this.dom = value;
},
div() {
var div = document.createElement(this.dom);
this._dom = document.body.appendChild(div);
return this._dom;
},
get() {
return this._dom;
}
};
var Fn = Object.create(Foo);
Fn.constructor = function (text, cssText) {
this.const.call(this, 'div');
this.text = text;
this.cssText = cssText;
};
Fn.set = function () {
this.div();
let div = this.get();
div.textContent = this.text;
Object.keys(this.cssText).forEach((name) => {
div.style.cssText += `${name}: ${this.cssText[name]}`;
});
};
var a = Object.create(Fn);
a.constructor('hi', { color: 'red', "font-size": '16px' });
a.set();
注意点:
1.在使用行为委托时要避免标识符的重名,因为行为委托是基于原型链,不同于“类”的设计模式,可以构造函数与实例对象同时定义相同的名称的标识符。
2.两个委托对象无法互相委托,假设一个a对象关联b对象,然后b对象再关联a对象,如果两个引用了一个两个对象都不存在的属性或者方法,那么就会在原型链上形成无限循环。
来源:https://www.cnblogs.com/boses/p/9575247.html
这是专门探索JavaScript及其构建组件的系列,在识别和描述核心元素的过程中,我们还分享了构建SessionStack时使用的一些经验法则,这是一个轻量级但健壮且高性能的JavaScript应用程序,以帮助用户实时查看和重现其Web应用程序的缺陷。
与其它类 Lisp 语言不同,不支持宏是 JavaScript 与生俱来的一个问题,这是因为宏会在编译时操作语法树,而这在像 JavaScript 这样的语言中几乎是不可能的。
接口返回的是int类型的秒数,在前端显示要求拼接为时分秒显示,这篇文章主要讲解实现js秒数转换成时分秒的方法。
javascript如何获取ul中li的个数?下面本篇文章就来给大家介绍一下使用javascript获取ul中li个数的方法,希望对大家有所帮助。
理解javascript中浮点数计算不精准的原因,如何解决浮点数的四则运算(加减乘除)。js中除了toFixed方法以外的实现方法总汇
最近阅读《JavaScript忍者秘籍》看到了一种有趣的函数:自记忆函数。记忆化(memoization)是一种构建函数的处理过程,能够记住上次计算结果,当函数计算得到结果时,就将该结果按照参数存储起来。
&、|、~都是位操作符,而&&、|、~|都是逻辑操作!。&&是逻辑与运算符假前真后,||是逻辑或运算符真前假后,&是按位与操作两个数值的个位分别相与,同时为1才得1,只要一个为0就为0。
expression 是一个要计算的 Javascript 标准的表达式。表达式外侧的圆括号是可选的,鉴于规范化,以及养成好习惯,建议写上去。当我们使用 void 操作符指定超级链接时,表达式会被计算但是不会在当前文档处装入任何内容。
delete关键字的作用:删除对象的属性 语法:delete 对象.属性、可以删除没有使用var关键字声明的全局变量(直接定义在window上面的属性)、删除数组元素、不能删除内置对象的属性、不能直接删除从原型上继承的属性
Performance提供的方法可以灵活使用,获取到页面加载等标记的耗时情况。Performance.timing属性对象提供了浏览器从打开网页到加载完成之间各个节点的耗时数据,包括重定向开始、DNS查询、浏览器响应数据、DOM解析等相关节点
内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!