原型即对象(以及认识这点的重要性)
function Person (first, last) {this.rename(first, last); }Person.prototype.fullName = function fullName () {return this.firstName + " " + this.lastName; };Person.prototype.rename = function rename (first, last) {this.firstName = first;this.lastName = last;return this; }
class Person {constructor (first, last) {this.rename(first, last);}fullName () {return this.firstName + " " + this.lastName;}rename (first, last) {this.firstName = first;this.lastName = last;return this;} };
不错。但究其本质,你仍然不过编写了一个名为 Person 的构造函数,并且 Person.prototype 是这样的一个对象:
{fullName: function fullName () {return this.firstName + " " + this.lastName;},rename: function rename (first, last) {this.firstName = first;this.lastName = last;return this;} }
原型即对象
function Person (first, last) {this.rename(first, last); }Object.assign(Person.prototype, {fullName: function fullName () {return this.firstName + " " + this.lastName;},rename: function rename (first, last) {this.firstName = first;this.lastName = last;return this;} })
当然,我们也可以使用紧凑方法声明语法
function Person (first, last) {this.rename(first, last); }Object.assign(Person.prototype, {fullName () {return this.firstName + " " + this.lastName;},rename (first, last) {this.firstName = first;this.lastName = last;return this;} })
mixins
class Person {constructor (first, last) {this.rename(first, last);}fullName () {return this.firstName + " " + this.lastName;}rename (first, last) {this.firstName = first;this.lastName = last;return this;} };Object.assign(Person.prototype, {addToCollection (name) {this.collection().push(name);return this;},collection () {return this._collected_books || (this._collected_books = []);} })
上例中我们将书籍收集相关的方法糅合到了 Person 类上。这点非常不错,因为我们得以让代码具有 point-free 风格,同时命名方面也很棒。
const BookCollector = {addToCollection (name) {this.collection().push(name);return this;},collection () {return this._collected_books || (this._collected_books = []);} };class Person {constructor (first, last) {this.rename(first, last);}fullName () {return this.firstName + " " + this.lastName;}rename (first, last) {this.firstName = first;this.lastName = last;return this;} };Object.assign(Person.prototype, BookCollector);
只要愿意,这一过程可一直进行
const BookCollector = {addToCollection (name) {this.collection().push(name);return this;},collection () {return this._collected_books || (this._collected_books = []);} };const Author = {writeBook (name) {this.books().push(name);return this;},books () {return this._books_written || (this._books_written = []);} };class Person {constructor (first, last) {this.rename(first, last);}fullName () {return this.firstName + " " + this.lastName;}rename (first, last) {this.firstName = first;this.lastName = last;return this;} };Object.assign(Person.prototype, BookCollector, Author);
为什么 mixins 可能有用武之地
为什么要知道这些
转载于:https://www.cnblogs.com/averyby/p/10021383.html
原型即对象(以及认识这点的重要性)相关推荐
- 有原型的对象和没有原型的对象
在学习Mobx时接触到了有原型对象和没原型的对象概念 声明一个对象 1 let o = Object.create({}) // 方法一 2 let o = {} // 方法二3 let o = Ob ...
- Prototype(原型)--对象创建模式
Prototype(原型)–对象创建模式 一.意图 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 二.动机 1.在软件系统中,经常面临着"某些结构复杂的对象"的 ...
- JavaScript学习(四十九)—构造方法、原型、对象图解
JavaScript学习(四十九)-构造方法.原型.对象图解
- JavaScript中的原型和对象机制
1 对象相关的一些语言特性 1.1 一切皆为对象 JavaScript里所有的东西都是对象. 对象是属性的集合. 数字, 字符串, 布尔值等原始值是"伪对象", 它们同样拥有属性, ...
- 设计模式_4_原型模式(对象的拷贝)
原形模式(PrototypePattern, 创建型模式,创建重复对象且保证性能, 对象的克隆) 通常使用原型模式创建一个原型接口, 用于获取创建对象的克隆, 对于浅拷贝与深拷贝不用纠结, 他们二者的 ...
- 当对象与原型有相同的属性,调用时的上下文指向问题
在查阅对象自定义事件及其触发实现的过程中,发现了如下代码: 即:构造函数的原型可以是其他构造方法new出来的对象实例,虽然理解构造函数原型就是一个对象,但通常都是用对象直接量来指定,如:F.proto ...
- 043_对象构造器和原型
1. 对象类型(蓝图)(类) 1.1. 通过文本和new Object()的方式, 我们只能创建单一对象. 1.2. 有时我们需要创建相同"类型"的许多对象的"蓝图&qu ...
- 关于对象、构造函数、原型、原型链、继承
对象: 在传统的面向过程的程序设计中,会造成函数或变量的冗余.而js中对象的目的是将所有的具有相同属性或行为的代码整合到一起,形成一个集合,这样就会方便管理,例如: var person1={name ...
- [js高手之路]原型对象(prototype)与原型链相关属性与方法详解
一,instanceof: instanceof检测左侧的__proto__原型链上,是否存在右侧的prototype原型. 我在之前的两篇文章 [js高手之路]构造函数的基本特性与优缺点 [js高手 ...
最新文章
- 15岁成杀人犯,监狱里学编程,37岁获释后年薪70万
- 计算机网络 --- 数据链路层CSMA/CD协议
- 巨星陨落,光芒永存—回顾霍金对人工智能的思考
- mysql 复制功能_MySQL实现主从复制功能
- Redis集群部署(半自动)
- jzoj 6301. 普及组
- ExtendSim 10.0.8发布于 2021年8月7日
- 2020年东三省数学建模联赛(辽宁赛区)获奖名单
- hdu1728 逃离迷宫
- cin.get()的用法
- one choise or a complain
- CSS 边框 圆角 盒子阴影 圆角 solid dotted dashed
- 通过SqlDbx导出*.sql
- CentOS 6.3安装chrome
- kubernetes-----pod资源创建与Harbor私有仓库
- 关于大象,冰箱和软件项目报价的故事
- SAP-MM科目自动分配解析-(4-1)- 物料的配置影响
- node文件系统 常用文件处理方法
- 大疆飞行模拟器 下载、安装及使用教程
- 新唐单片机keil驱动问题