javascript原型_在JavaScript中冻结原型时会发生什么

javascript原型

Have you wondered what happens when you freeze the prototype of an object? Let's find out together.

您是否想过冻结对象的原型时会发生什么? 让我们一起找出答案。

对象 (Objects)

In JavaScript, objects are dynamic collections of properties with a “hidden” property. We start by creating such an object using the object literal syntax.

在JavaScript中,对象是具有“隐藏”属性的动态属性集合。 我们首先使用对象文字语法创建此类对象。

const counter = {count: 0,increment(){this.count += 1;return this.count;},decrement(){this.count -= 1;return this.count}  
}console.log(counter.increment())

counter is an object with a field and two methods operating on it.

counter是一个具有字段和在其上操作的两种方法的对象。

样机 (Prototypes)

Objects can inherit properties from prototypes. As a matter of fact, the counter object already inherits from the Object.prototype object.

对象可以从原型继承属性。 实际上, counter对象已经从Object.prototype对象继承。

For example we can call the toString() method on the counter object even if we haven’t defined it.

例如,即使没有定义,我们也可以在counter对象上调用toString()方法。

counter.toString();

The best way to work with prototypes is to extract out the methods in the prototype and then share them between all objects having the same behavior. Let’s do that using Object.create().

处理原型的最佳方法是提取原型中的方法,然后在具有相同行为的所有对象之间共享它们。 让我们使用Object.create()做到这一点。

const counterPrototype = {increment(){this.count += 1;return this.count;},decrement(){this.count -= 1;return this.count}
}const counter = Object.create(counterPrototype);
counter.count = 0;
console.log(counter.increment())
//1

The Object.create() creates a new object, using an existing object as the prototype of the new object.  counter has  counterPrototype as its prototype.

Object.create()使用现有对象作为新对象的原型创建一个新对象。 counter具有counterPrototype作为其原型。

The prototype system is flexible but has some downfalls. All properties are public and can be changed.

原型系统很灵活,但存在一些缺点。 所有属性都是公开的,可以更改。

For example, we can redefine the implementation of the increment() object in the counter object.

例如,我们可以在counter对象中重新定义increment()对象的实现。

const counter = Object.create(counterPrototype);
counter.count = 0;counter.increment = function(){console.log('increment')
}console.log(counter.increment());
//"increment"

冻结原型 (Freezing Prototypes)

Let’s see what happens if we freeze the prototype. Freezing an object doesn’t allow us to add, remove, or change its properties.

让我们看看如果冻结原型会发生什么。 冻结对象不允许我们添加,删除或更改其属性。

const counterPrototype = Object.freeze({increment(){this.count += 1;return this.count;},decrement(){this.count -= 1;return this.count}
});counterPrototype.increment = function(){console.log('increment')
}
//Cannot assign to read only property 'increment' of object '#'

The Object.freeze() freezes an object. A frozen object can no longer be changed. We cannot add, edit, or remove properties from it.

Object.freeze()冻结对象。 冻结的对象无法再更改。 我们无法添加,编辑或删除其中的属性。

Now take a look at what happens when trying to change the method in the counter object inheriting from counterPrototype.

现在来看一下尝试更改从counterPrototype继承的counter对象中的方法时会发生什么。

const counter = Object.create(counterPrototype);
counter.count = 0;counter.increment = function(){console.log('increment')
}
//Cannot assign to read only property 'increment' of objectconsole.log(counter.increment());
//1

As you can see now that the prototype is frozen we are not able to change the increment() method in the counter object.

如您现在所见,原型已冻结,我们无法在counter对象中更改increment()方法。

回顾 (Recap)

Objects have a hidden property referring their prototype.

对象具有引用其原型的隐藏属性。

The prototype is usually used to keep the methods that are shared between different objects.

原型通常用于保留在不同对象之间共享的方法。

Freezing the prototype does not allow us to change those properties in the objects inheriting from that prototype. The other properties can be changed.

冻结原型不允许我们更改从该原型继承的对象中的那些属性。 其他属性可以更改。

Discover Functional JavaScript was named one of the best Functional Programming books by BookAuthority!

发现功能JavaScript被称为 BookAuthority 最好的函数式编程书籍

For more on applying functional programming techniques to React take a look at Functional React.

有关将函数式编程技术应用于React的更多信息,请查看Functional React 。

Learn functional React, in a project-based way, with Functional Architecture with React and Redux.

通过带有React和Redux的功能架构 ,以基于项目的方式学习功能性React

翻译自: https://www.freecodecamp.org/news/what-happens-when-you-freeze-a-prototype/

javascript原型

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390155.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

迟来的2017总结

明天就是年后第一天上班了(过年期间请了6天假), 打算今天写一下2017的总结,本来还想写2018的愿景的,不过想想还是算了,现在没什么想法,不想敷衍了事。 先贴一个2017的提升计划: http…

tomcat启动卡住

新部署的项目启动tomcat后一直停在org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.16,卡在了org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/tomcat/web…

怎样准备阿里技术面试_如何准备技术面试

怎样准备阿里技术面试In June 2020 I watched an inspiring talk by Anthony D. Mays, a technical coach and founder at Morgan Latimerco. He came on a Facebook Developer Circles Benin live session and talked about how to prepare for a technical interview. 2020年…

通过一个简单例子理解 RecyclerView.ItemDecoration

一、前言 RecyclerView 是从5.0推出的 MD 风格的控件。RecyclerView 之前有 ListView、GridView,但是功能很有限,例如 ListView 只能实现垂直方向上的滑动等。但是存在则合理,ListView 却没有被官方标记为 Deprecated,有兴趣的同学…

Entity Framework Logging and Intercepting Database Operations (EF6 Onwards)

参考官方文档:https://msdn.microsoft.com/en-us/library/dn469464(vvs.113).aspx转载于:https://www.cnblogs.com/liandy0906/p/8473110.html

面试题 17.14. 最小K个数

面试题 17.14. 最小K个数 设计一个算法&#xff0c;找出数组中最小的k个数。以任意顺序返回这k个数均可。 示例&#xff1a; 输入&#xff1a; arr [1,3,5,7,2,4,6,8], k 4 输出&#xff1a; [1,2,3,4] 提示&#xff1a; 0 < len(arr) < 1000000 < k < min(1…

这是您现在可以免费获得的115张Coursera证书(在冠状病毒大流行期间)

At the end of March, the world’s largest Massive Open Online Course provider Coursera announced that they are offering 100 free courses in response to the impact of the COVID-19 pandemic. 3月底&#xff0c;全球最大的大规模在线公开课程提供商Coursera 宣布 &a…

由浅入深理解----java反射技术

java反射机制详解 java反射机制是在运行状态下&#xff0c;对任意一个类可以获取该类的属性和方法&#xff0c;对任意一个对象可以调用其属性和方法。这种动态的获取信息和调用对象的方法的功能称为java的反射机制 class<?>类&#xff0c;在java.lang包下面&#xff0c;…

【VMware vSAN 6.6】5.5.Update Manager:vSAN硬件服务器解决方案

目录 1. 简介 1.1.适用于HCI的企业级存储2. 体系结构 2.1.带有本地存储的服务器2.2.存储控制器虚拟系统套装的缺点2.3.vSAN在vSphere Hypervisor中自带2.4.集群类型2.5.硬件部署选项3. 启用vSAN 3.1.启用vSAN3.2.轻松安装3.3.主动测试4. 可用性 4.1.对象和组件安置4.2.重新构建…

5848. 树上的操作

给你一棵 n 个节点的树&#xff0c;编号从 0 到 n - 1 &#xff0c;以父节点数组 parent 的形式给出&#xff0c;其中 parent[i] 是第 i 个节点的父节点。树的根节点为 0 号节点&#xff0c;所以 parent[0] -1 &#xff0c;因为它没有父节点。你想要设计一个数据结构实现树里面…

了解如何通过Python使用SQLite数据库

SQLite is a very easy to use database engine included with Python. SQLite is open source and is a great database for smaller projects, hobby projects, or testing and development.SQLite是Python附带的非常易于使用的数据库引擎。 SQLite是开源的&#xff0c;是用于…

32位JDK和64位JDK

32位和64位系统在计算机领域中常常提及&#xff0c;但是仍然很多人不知道32位和64位的区别&#xff0c;所以本人在网上整理了一些资料&#xff0c;并希望可以与大家一起分享。对于32位和64位之分&#xff0c;本文将分别从处理器&#xff0c;操作系统&#xff0c;JVM进行讲解。 …

中小企业如何选择OA协同办公产品?最全的对比都在这里了

对于中小企业来说&#xff0c;传统的OA 产品&#xff0c;如泛微、蓝凌、致远、华天动力等存在价格高、使用成本高、二次开发难等特点&#xff0c;并不适合企业的协同管理。 国内OA市场也出现了一批轻便、低价的OA产品&#xff0c;本文针对以下几款适合中小企业的OA产品在功能、…

python缓冲区_如何在Python中使用Google的协议缓冲区

python缓冲区When people who speak different languages get together and talk, they try to use a language that everyone in the group understands. 当说不同语言的人聚在一起聊天时&#xff0c;他们会尝试使用小组中每个人都能理解的语言。 To achieve this, everyone …

PowerDesigner16中的对象无效,不允许有扩展属性 问题的解决

PowerDesigner16中的对象无效&#xff0c;不允许有扩展属性 消息 15135&#xff0c;级别 16&#xff0c;状态 1&#xff0c;过程 sp_addextendedproperty&#xff0c;第 37 行 对象无效。XXXXXXX 不允许有扩展属性&#xff0c;或对象不存在。 把 execute sp_addextendedpropert…

Elasticsearch学习(2)—— 常见术语

为什么80%的码农都做不了架构师&#xff1f;>>> cluster (集群)&#xff1a;一个或多个拥有同一个集群名称的节点组成了一个集群。每个集群都会自动选出一个主节点&#xff0c;如果该主节点故障&#xff0c;则集群会自动选出新的主节点来替换故障节点。 node (节点…

67. 二进制求和

67. 二进制求和 给你两个二进制字符串&#xff0c;返回它们的和&#xff08;用二进制表示&#xff09;。 输入为 非空 字符串且只包含数字 1 和 0。 示例 1: 输入: a “11”, b “1” 输出: “100” 示例 2: 输入: a “1010”, b “1011” 输出: “10101” 提示&…

前端开发有哪些技术栈要掌握_为什么要掌握前端开发的这四个主要概念

前端开发有哪些技术栈要掌握After working as a front-end developer for three years, I have been able to summarize what I feel are the four major concepts of front-end development. Knowing and studying these four areas will make you stand out from the crowd.在…

python中的序列化与反序列化

之前&#xff0c;在学习python时&#xff0c;一直弄不明白pickle和json模块的序列化和反序例化之间的区别和用法&#xff0c;最近闲来有时间&#xff0c;重新研究了这两个模块&#xff0c;也算是基本搞明白他们之中的区别了。 用于序列化的两个模块&#xff0c; json&#xff0…

1114. 按序打印

1114. 按序打印 我们提供了一个类&#xff1a; public class Foo { public void first() { print(“first”); } public void second() { print(“second”); } public void third() { print(“third”); } } 三个不同的线程 A、B、C 将会共用一个 Foo 实例。 一个将会调用 …