javascript關鍵字_讓我們揭開JavaScript的“ new”關鍵字的神秘面紗

javascript關鍵字

by Cynthia Lee

辛西婭·李(Cynthia Lee)

讓我們揭開JavaScript的“ new”關鍵字的神秘面紗 (Let’s demystify JavaScript’s ‘new’ keyword)

Over the weekend, I completed Will Sentance’s JavaScript: The Hard Parts. It might not sound like the most glorious way to spend a weekend, but I actually found it pretty fun and relaxing to complete the course. It touched on functional programming, higher-order functions, closures, and asynchronous JavaScript.

上周末,我完成了Will Sentance的JavaScript:The Hard Parts 。 聽起來可能不是周末度過的最光榮的方式,但實際上,我覺得這很有趣且輕松,可以完成課程。 它涉及函數式編程,高階函數,閉包和異步JavaScript。

For me, the highlight of the course was how he expanded on the JavaScript approaches to Object-Oriented Programming (OOP) and demystified the magic behind the new operator. I now have a well-rounded understanding of what goes on under the hood when the new operator is used.

對我而言,本課程的重點是他如何擴展JavaScript面向對象編程(OOP)的方法,并揭開了操作符背后的神秘面紗。 現在,我對使用操作員時幕后情況有了全面的了解。

JavaScript中的面向對象編程 (Object-Oriented Programming in JavaScript)

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of “objects.” Data and functions (attributes and methods) are bundled within an object.

面向對象編程(OOP)是基于“對象”概念的編程范例。 數據和功能(屬性和方法)捆綁在一個對象中。

An object in JavaScript is a collection of key-value pairs. These key-value pairs are properties of the object. A property can be an array, a function, an object itself or any primitive data type such as strings or integers.

JavaScript中的對象是鍵值對的集合。 這些鍵值對是對象的屬性。 屬性可以是數組,函數,對象本身或任何原始數據類型,例如字符串或整數。

What techniques do we have in our JavaScript toolbox for object creation?

我們JavaScript工具箱中有哪些技術可用于對象創建?

Let’s assume that we are creating users in a game that we just designed. How would we store user details such as their names, points, and implement methods such as an increment in points? Here are two options for basic object creation.

假設我們在剛剛設計的游戲中創建用戶。 我們將如何存儲用戶詳細信息(例如他們的姓名,積分)以及實現方法(例如增加積分)? 這是用于基本對象創建的兩個選項。

選項1-對象文字表示法 (Option 1 — Object Literal Notation)

let user1 = {name: "Taylor",points: 5,increment: function() {user1.points++;}
};

A JavaScript object literal is a list of name-value pairs wrapped in curly braces. In the example above, the object ‘user1’ is created, and the associated data is stored within it.

JavaScript對象文字是用花括號括起來的名稱-值對的列表。 在上面的示例中,創建了對象“ user1”,并將關聯的數據存儲在其中。

選項2 — Object.create() (Option 2 — Object.create())

Object.create(proto, [ propertiesObject ])

Object.create(proto, [ propertiesObject ])

Object.create methods accept two arguments:

Object.create方法接受兩個參數:

  1. proto: the object which should be the prototype of the newly-created object. It has to be an object or null.

    proto:應該是新創建對象的原型的對象。 它必須是一個對象或null。
  2. propertiesObject: properties of the new object. This argument is optional.

    propertiesObject:新對象的屬性。 此參數是可選的。

Basically, you pass into Object.create an object that you want to inherit from, and it returns a new object that inherits from the object you passed into it.

基本上,您將傳遞給Object.create一個想要繼承的對象,然后返回一個新對象,該對象繼承Object.create遞給它的對象。

let user2 = Object.create(null);user2.name = "Cam";
user2.points = 8;
user2.increment = function() {user2.points++;
}

The basic object creation options above are repetitive. It requires each one to be manually created.

上面的基本對象創建選項是重復的。 它要求每個手動創建。

How do we overcome this?

我們如何克服這個問題?

解決方案 (Solutions)

解決方案1 ??—使用函數生成對象 (Solution 1 — Generate objects using a function)

A simple solution is to write a function to create new users.

一個簡單的解決方案是編寫一個函數來創建新用戶。

function createUser(name, points) {let newUser = {};newUser.name = name;newUser.points = points;newUser.increment = function() {newUser.points++;};return newUser;
}

To create a user, you would now enter the information in parameters of the function.

要創建用戶,您現在將在功能參數中輸入信息。

let user1 = createUser("Bob", 5);
user1.increment();

However, the increment function in the example above is just a copy of the original increment function. This is not a good way to write your code, as any potential changes to the function will need to be done manually for each object.

但是,上面示例中的增量函數只是原始增量函數的副本。 這不是編寫代碼的好方法,因為對該功能的任何潛在更改都需要針對每個對象手動進行。

解決方案2-使用JavaScript的原型性質 (Solution 2 — Use the prototypal nature of JavaScript)

Unlike object-oriented languages such as Python and Java, JavaScript does not have classes. It uses the concept of prototypes and prototype chaining for inheritance.

與Python和Java等面向對象的語言不同,JavaScript沒有類。 它使用原型和原型鏈的概念進行繼承。

When you create a new array, you automatically have access to built-in methods such as Array.join, Array.sort, and Array.filter. This is due to the fact that array objects inherit properties from Array.prototype.

創建新數組時,可以自動訪問內置方法,例如Array.joinArray.sortArray.filter 。 這是由于數組對象繼承了Array.prototype的屬性。

Every JavaScript function has a prototype property, which is empty by default. You can add functions to this prototype property, and in this form, it is known as a method. When an inherited function is executed, the value of this points to the inheriting object.

每個JavaScript函數都有一個prototype屬性,默認情況下為空。 您可以向該原型屬性添加函數,并且以這種形式將其稱為方法。 當執行繼承的函數時,此值指向繼承的對象。

function createUser(name, points) {let newUser = Object.create(userFunction);newUser.name = name;newUser.points = points;return newUser;
}let userFunction = {increment: function() {this.points++};login: function() {console.log("Please login.")};
}let user1 = createUser("Bob", 5);
user1.increment();

When the user1 object was created, a prototype chain bond with userFunction was formed.

創建user1對象時,形成了帶有userFunction的原型鏈鍵。

When user1.increment() is in the call stack, the interpreter will look for user1 in the global memory. Next, it will look for the increment function, but will not find it. The interpreter will look at the next object up the prototype chain and will find the increment function there.

user1.increment()位于調用堆棧中時,解釋器將在全局內存中查找user1。 接下來,它將查找增量函數,但找不到它。 解釋器將查看原型鏈中的下一個對象,并在其中找到增量函數。

解決方案3- 關鍵字和關鍵字 (Solution 3 — new and this keywords)

The new operator is used to create an instance of an object which has a constructor function.

new運算符用于創建具有構造函數的對象的實例。

When we call the constructor function with new, we automate the following actions:

當我們使用new調用構造函數時,我們將自動執行以下操作:

  • A new object is created

    創建一個新對象
  • It binds this to the object

    它結合this對象

  • The constructor function’s prototype object becomes the __proto__ property of the new object

    構造函數的原型對象成為新對象的__proto__屬性。
  • It returns the object from the function

    它從函數返回對象

This is fantastic, because the automation results in less repetitive code!

這太棒了,因為自動化可以減少重復代碼!

function User(name, points) {this.name = name; this.points = points;
}
User.prototype.increment = function(){this.points++;
}
User.prototype.login = function() {console.log(“Please login.”)
}let user1 = new User(“Dylan”, 6);
user1.increment();

By using the prototype pattern, each method and property is added directly on the object’s prototype.

通過使用原型模式,每個方法和屬性都直接添加到對象的原型上。

The interpreter will go up the prototypal chain and find the increment function under the prototype property of User, which itself is also an object with the information inside it. Remember — All functions in JavaScript are also objects. Now that the interpreter has found what it needs, it can create a new local execution context to run user1.increment().

解釋器將沿著原型鏈向上移動,并在User的prototype屬性下找到增量函數,該屬性本身也是一個內部包含信息的對象。 請記住,JavaScript中的所有函數也是對象 。 現在,解釋器已經找到了所需的內容,它可以創建一個新的本地執行上下文來運行user1.increment()

Side note: Difference between __proto__ and prototype

旁注:__ proto__和原型之間的區別

If you are already getting confused about __proto__ and prototype, don’t worry! You are far from the only one to be confused about this.

如果您已經對__proto__和原型感到困惑,請放心! 您遠非唯一對此感到困惑的人。

Prototype is a property of the constructor function that determines what will become the __proto__ property on the constructed object.

原型是構造函數的屬性,該函數確定在構造對象上將成為__proto__屬性的內容。

So, __proto__ is the reference created, and that reference is known as the prototype chain bond.

因此,__ proto__是創建的引用,該引用稱為原型鏈鍵。

解決方案4-ES6“語法糖” (Solution 4 — ES6 ‘syntactic sugar’)

Other languages allow us to write our shared methods within the object “constructor” itself. ECMAScript6 introduced the class keyword, which allows us to write classes that resemble normal classes of other classical languages. In reality, it is syntactic sugar over JavaScript’s prototypal behavior.

其他語言允許我們在對象“構造函數”本身內編寫共享方法。 ECMAScript6引入了class關鍵字,該關鍵字使我們能夠編寫類似于其他古典語言的普通類的類。 實際上,它是JavaScript原型行為的語法糖。

class User {constructor(name, points) {this.name = name;this.points = points;}increment () {this.points++;}login () {console.log("Please login.")}
}let user1 = new User("John", 12);
user1.increment();

In solution 3, the associated methods were precisely implemented using User.prototype.functionName. In this solution, the same results are achieved but the syntax looks cleaner.

在解決方案3中,使用User.prototype.functionName精確實現了相關方法。 在此解決方案中,可以達到相同的結果,但是語法看起來更簡潔。

結論 (Conclusion)

We have now learned more about the different options we have in JavaScript to create objects. While class declarations and the new operator are relatively easy to use, it is important to understand what is automated.

現在,我們已經了解了更多有關JavaScript創建對象的不同選項的信息。 雖然聲明和 操作員相對易于使用,重要的是要了解什么是自動化的。

To recap, the following actions are automated when the constructor function is called with new:

概括地說,當用new調用構造函數時,以下動作是自動的

  • A new object is created

    創建一個新對象
  • It binds this to the object

    它結合this對象

  • The constructor function’s prototype object becomes the __proto__ property of the new object

    構造函數的原型對象成為新對象的__proto__屬性。
  • It returns the object from the function

    它從函數返回對象

Thanks for reading my article, and clap if you liked it! Check out my other articles like How I built my Pomodoro Clock app, and the lessons I learned along the way.

感謝您閱讀我的文章,如果喜歡您也可以鼓掌! 查閱其他文章,例如“我如何構建Pomodoro Clock應用程序”,以及我在此過程中學到的課程 。

翻譯自: https://www.freecodecamp.org/news/demystifying-javascripts-new-keyword-874df126184c/

javascript關鍵字

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/394357.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/394357.shtml
英文地址,請注明出處:http://en.pswp.cn/news/394357.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

查看 rabbitmq 啟動websocket 提示404_RabbitMQ在windows下安裝(筆記)

先保證Java開發環境一切正常,【jdk,maven】,然后下載兩個文件,1,下載OTPhttps://www.rabbitmq.com/download.html 下載地址下載RabbitMQ Downloading and Installing RabbitMQ:地址安裝沒有別的操作,一直下一步就好;2&…

[Leetcode] Longest Valid Parentheses

找出字串裡最長的合法括號組 簡單說&#xff0c;一樣stack搜尋合法parenth&#xff0c;把不合法的 ( & ) index 紀錄下來&#xff0c;最後算index間的差值取最大就是最長的 public class Solution{/// <summary>/// 把不合法的( )的index記下來&#xff0c;取最長的差…

leetcode35. 搜索插入位置(二分搜索)

給定一個排序數組和一個目標值&#xff0c;在數組中找到目標值&#xff0c;并返回其索引。如果目標值不存在于數組中&#xff0c;返回它將會被按順序插入的位置。 你可以假設數組中無重復元素。 示例 1: 輸入: [1,3,5,6], 5 輸出: 2 代碼 class Solution {public int sear…

[deviceone開發]-do_Album的簡單示例

一、簡介do_Album用來打開手機系統提供的相冊&#xff0c;能選擇一張或多張圖片返回給開發者&#xff0c;通常相冊的圖片比較大&#xff0c;要經過縮放。有的時候用戶也需要把別的地方獲取到到圖片收藏到系統相冊。這個示例簡單展示這個組件的2個基本功能。二、效果圖三、相關下…

公辦低分二本_這六所公辦二本高校的計算機類相關專業值得低分段考生選擇

邯鄲學院——計算機科學與技術近年來&#xff0c;邯鄲學院著力強化“以本為本”理念,堅持“學生中心”“產出導向”原則&#xff0c;加強學科專業建設&#xff0c;獲批國家級特色專業1個&#xff0c;省級重點發展學科3個&#xff0c;省級一流專業7個&#xff0c;英語等3個專業入…

用戶體驗改善案例_用戶體驗案例研究:建立更好的體驗(重新設計“和平航空”網站)...

用戶體驗改善案例by Peace Ojemeh (Perrie)由Peace Ojemeh(Perrie) 用戶體驗案例研究&#xff1a;建立更好的體驗(重新設計“和平航空”網站) (A UX case study: Building a better experience (Re-designing the Air Peace Airline website)) Traveling by air is always an …

[轉]FFMPEG調節音頻的音量大小,混音

鏈接&#xff1a;https://blog.csdn.net/nil_lu/article/details/52078488 轉載于:https://www.cnblogs.com/zifeiy/p/10675734.html

局域網即時通訊軟件_無線局域網中,安卓手機和電腦的資源如何實現互傳互訪?...

安卓手機和電腦之間的資源共享&#xff0c;可實現的方案有很多&#xff0c;例如&#xff1a;方案一是各種官方或第三方出品的“XX手機助手”軟件。優點是直連的傳輸速率最高&#xff1b;缺點一是手機和電腦必須連在一起&#xff0c;相當不方便&#xff0c;缺點二是萬一中途發生…

leetcode516. 最長回文子序列(動態規劃)

***給定一個字符串 s &#xff0c;找到其中最長的回文子序列&#xff0c;并返回該序列的長度。***可以假設 s 的最大長度為 1000 。 示例 1: 輸入: “bbbab” 輸出: 4 一個可能的最長回文子序列為 “bbbb”。 解題思路 數組含義&#xff1a;dp[i][j]子串&#xff08;i&#…

Ubuntu 14.04 FTP服務器--vsftpd的安裝和配置

更新源列表 打開"終端窗口"&#xff0c;輸入"sudo apt-get update"-->回車-->"輸入當前登錄用戶的管理員密碼"-->回車,就可以了。如果不運行該命令&#xff0c;直接安裝vsftpd,會出現"有 幾個軟件包無法下載&#xff0c;您可以運…

校驗電話號碼 手機號碼正則表達式

2019獨角獸企業重金招聘Python工程師標準>>> 電話號碼 手機號碼 等準確詳細 正則表達式電話號碼正則表達式 &#xff08;支持手機號碼&#xff0c;3-4位區號&#xff0c;7-8位直播號碼&#xff0c;1&#xff0d;4位分機號&#xff09; ((\d{11})|^((\d{7,8})|(\d{4}…

期刊投稿狀態_SCI投稿全過程解析及拒稿后處理對策

之前給大家介紹了如果使用人工智能來提高SCI寫作效率的神器&#xff0c;相信大家對SCI寫作已經很有信心了。但有些小伙伴后臺說對投稿過程很沒有概念&#xff0c;不同期刊不同狀態。那么今天我們就對SCI投稿過程、投稿狀態做一個總結和解析以及拒稿后處理對策及接受后期相關問答…

cake php_如何(以及為什么)在Swinject中使用Cake Pattern

cake phpby Peter-John Welcome由Peter-John Welcome 如何(以及為什么)在Swinject中使用Cake Pattern (How (and why) to use the Cake Pattern with Swinject) In my previous article, I showed how we can use the Cake Pattern to do dependency injection without any li…

運用Appium 實現添加微信好友自動化

本文為原創文章&#xff0c;如需轉載請注明出處. 任務&#xff1a;實現批量添加微信好友自動化。 任務分析&#xff1a;1.首先要實現添加單個好友步驟自動化。 2.實現腳本讀取Excel里的值。 3.參數化好友電話號碼或者昵稱。 PS:代碼采用POM(Page Object Model)便于后續維護 數…

pdf.js瀏覽中文pdf亂碼的問題解決

由于項目中需要支持移動設備在線瀏覽pdf&#xff0c;蘋果還好&#xff0c;天生支持&#xff0c;但是安卓中就不行了&#xff0c;需要第三方組件的支持。 這里就找到了pdf.js&#xff0c;由于pdf數據太多&#xff0c;開始的時候沒法一一測試&#xff0c;所以隨便測試打開了幾篇沒…

python導入sas數據集_運用import過程進行SAS數據導入完全實用教程

運用import過程進行SAS數據導入完全實用教程1 單個規范格式文件導入。對單個文件進行導入是我們遇到最多的情況&#xff0c;主要有以下幾種&#xff1a;1.1 對指定分隔符(’|’&#xff0c;’’&#xff0c;’!’&#xff0c;’ab’等)數據的導入&#xff0c;這里以’!’為例de…

【效率專精系列】善用API統一描述語言提升RestAPI開發效率

團隊內部RestAPI開發采用設計驅動開發的模式&#xff0c;即使用API設計文檔解耦前端和后端的開發過程&#xff0c;雙方只在聯調與測試時耦合。在實際開發和與前端合作的過程中&#xff0c;受限于眾多因素的影響&#xff0c;開發效率還有進一步提高的空間。本文的目的是優化工具…

leetcode劍指 Offer 14- I. 剪繩子(動態規劃)

給你一根長度為 n 的繩子&#xff0c;請把繩子剪成整數長度的 m 段&#xff08;m、n都是整數&#xff0c;n>1并且m>1&#xff09;&#xff0c;每段繩子的長度記為 k[0],k[1]…k[m-1] 。請問 k[0]k[1]…*k[m-1] 可能的最大乘積是多少&#xff1f;例如&#xff0c;當繩子的…

數據包提取文件_航測怎樣高效提取無人機POS航點數據

無限創新工作室研發的POS數據記錄儀是一款采集飛控POS 數據并管理的設備&#xff0c;它將飛控 POS 點數據進行記錄&#xff0c;形成單獨的POS 數據記錄TXT 文本&#xff0c;并獨立存儲于內存卡&#xff0c;可通過USB、U 盤或內存卡形式對數據進行讀取。通過對相機進行拍照控制和…

點擊刪除表格中的行并提交到數據庫

html中&#xff1a; <el-table-column prop"operation" label"操作" width"170"> <template slot-scope"scope"> <el-button size"small" type"danger" click"deleteRow(scope.$index,s…