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.pswp.cn/news/390155.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390155.shtml
英文地址,請注明出處:http://en.pswp.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 實例。 一個將會調用 …