sql語句中的in用法示例_示例中JavaScript in操作符

sql語句中的in用法示例

One of the first topics you’ll come across when learning JavaScript (or any other programming language) are operators.

學習JavaScript(或任何其他編程語言)時遇到的第一個主題之一是運算符。

The most common operators are the arithmetic, logical, and comparison operators. But did you know that JavaScript has an in operator?

最常見的運算符是算術,邏輯和比較運算符。 但是您知道JavaScript具有in運算符嗎?

If you didn't, don’t fret. I just came across it recently while searching for a solution to a problem on Google.

如果沒有,請不要擔心。 我最近在Google上尋找問題的解決方案時碰到了它。

In this article, you’ll learn exactly what the JavaScript in operator does, when to use it, and how to use it.

在本文中,您將確切地了解JavaScript in運算符的功能,何時使用它以及如何使用它。

JavaScript in運算符到底是什么? (What exactly is the JavaScript in operator?)

The JavaScript in operator is used to check if a specified property exists in an object or in its inherited properties (in other words, its prototype chain). The in operator returns true if the specified property exists.

JavaScript in運算符用于檢查對象或其繼承的屬性(即,其原型鏈)中是否存在指定的屬性。 如果指定的屬性存在,則in運算符將返回true

The JavaScript prototype chain is how objects or object instances have access to properties and methods that were not originally theirs. These objects inherit properties and methods defined in their constructors or prototypes, which can be accessed through their __proto__ property.

JavaScript原型鏈是對象或對象實例如何訪問最初不是其屬性和方法的屬性。 這些對象繼承了在其構造函數或原型中定義的屬性和方法,可以通過其__proto__屬性對其進行訪問。

This article assumes that you have a basic understanding of what objects are, how to create them, what they are used for, and how JavaScript inheritance works. If you don’t, this article on MDN should help.

本文假定您對什么是對象,如何創建它們,將它們用于什么以及JavaScript繼承如何工作有基本的了解。 如果您不這樣做,那么有關MDN的這篇文章應該會有所幫助。

何時在操作符中使用JavaScript (When to use the JavaScript in operator)

驗證對象上是否存在屬性 (To verify if a property exists on an object)

const car = {make: 'Toyota',model:'Camry',year: '2018',start: function() {console.log(`Starting ${this.make} ${this.model}, ${this.year}`);}
}'make' in car // Returns true.
'start' in car // Returns true.
'Toyota' in car // Returns false. 'Toyota' is not a property name, but a value.

驗證屬性是否被對象繼承。 (To verify if a property is inherited by an object.)

Let’s use the ES6 class syntax to create an object constructor. This would also apply to function constructors:

讓我們使用ES6類語法創建對象構造函數。 這也適用于函數構造函數:

class Car {constructor(make, model, year) {this.make = make;this.model = model;this.year = year;}start() {console.log(`Starting ${this.make} ${this.model}, ${this.year}`);}
}const toyota = new Car('Toyota', 'Camry', '2018');'start' in toyota;
/* Returns true as toyota is an instance of the Car object constructor. The toyota object therefore inherits all properties of the Car constructor. */'toString' in toyota;
/* Returns true. toString is a method property of the Object type, of which the Car constructor is an instance of. */

驗證數組中是否存在索引/鍵。 (To verify if an index/key exists on an array.)

You might be wondering, since we established that the JavaScript in operator can be used with objects, why can we also use it with arrays?

您可能想知道,既然我們確定JavaScript in操作符可以與對象一起使用,為什么我們也可以將其與數組一起使用?

Well, an array is actually a prototype (instance) of the Object type. In fact, everything in JavaScript is an instance of the Object type.

好吧,數組實際上是Object類型的原型(實例)。 實際上,JavaScript中的所有內容都是Object類型的實例。

That may sound crazy, but lets run a simple program in the browser's console to confirm.

這聽起來很瘋狂,但是讓我們在瀏覽器的控制臺中運行一個簡單的程序進行確認。

First, define an array and confirm if its an instance of the Object type using the instanceof operator:

首先,定義一個數組,并使用instanceof運算符確認其是否為Object類型的instanceof

const number = [2, 3, 4, 5];number instanceof Object // Returns true

Still in doubt? Type number into the console and press enter, then open up the output.

還是有疑問嗎? 鍵入number到控制臺,然后按回車,然后打開輸出。

You’ll notice a list of properties, one of which is __proto__ which points to Array. Opening that too and going down that list bring us to another __proto__ property with a value of Object.

您會注意到一系列屬性,其中一個是__proto__ ,它指向Array 。 也打開它并在列表中__proto__會帶我們到另一個具有Object值的__proto__屬性。

That shows that the number array is an instance of the Array type which is an instance of the Object type.

這表明number數組是Array類型的實例,而Array類型是Object類型的實例。

Now, back to using the in operator:

現在,回到使用in運算符:

const number = [2, 3, 4, 5];3 in number // Returns true.
2 in number // Returns true.5 in number // Returns false because 5 is not an existing index on the array but a value;'filter' in number
/* Returns true because filter is a method property on the Array type of which the number array is an instance of. The number array inherits the filter property.*/

驗證HTML元素上是否存在屬性 (To verify if a property exists on a Html element)

In Kirupa's article, Check If You Are On a Touch Enabled Device, he highlights this function:

在Kirupa的文章中,“ 檢查您是否在啟用觸摸的設備上” ,他強調了此功能:

function isTouchSupported() {var msTouchEnabled = window.navigator.msMaxTouchPoints;var generalTouchEnabled = "ontouchstart" in document.createElement("div");if (msTouchEnabled || generalTouchEnabled) {return true;}return false;
}

This function returns true if you are on a device that supports touch and returns false if you are on a device that doesn't support touch by checking if the properties window.navigator.msMaxTouchPoints and ontouchstart are present. These properties only exist on devices that are touch enabled.

如果您在支持觸摸的設備上,則此函數返回true如果您在不支持觸摸的設備上,則通過檢查屬性window.navigator.msMaxTouchPointsontouchstart是否存在,返回false 。 這些屬性僅存在于啟用觸摸的設備上。

Pretty straightforward!

非常簡單!

Lets focus on the highlighted line. Remember how we established that the in operator returns true if the specified property exists in an object? HTML elements used in JavaScript actually become instances of the Object type, hence the name "Document Object Model" or DOM.

讓我們專注于突出顯示的行。 還記得我們如何確定如果對象中存在指定的屬性,則in運算符將返回true ? JavaScript中使用HTML元素實際上成為Object類型的實例,因此名稱為“文檔對象模型”或DOM。

Of course, you might not believe me without some sort of proof. As before, let’s type some commands into the console.

當然,如果沒有任何證據,您可能不會相信我。 和以前一樣,讓我們??在控制臺中輸入一些命令。

Create a div element and list out its properties using console.dir():

創建一個div元素,并使用console.dir()列出其屬性:

const element = document.createElement('div');console.dir(element);

You'll then see the div element with its properties listed in the console.

然后,您將在控制臺中看到div元素及其屬性。

Open the drop down and you’ll notice that it has a __proto__ property of HtmlDivElement. Open that and you’ll find another __proto__ property of HtmlElement, then Element, Node, Eventtarget, and finally Object.

打開下拉列表,您會注意到它具有HtmlDivElement__proto__屬性。 打開它,您將找到HtmlElement另一個__proto__屬性, 然后是ElementNodeEventtarget ,最后是Object

Also run:

同時運行:

element instanceof Object

This will return true, showing that the div element is an instance of the Object type, which is why the in operator can be used on it.

這將返回true ,表明div元素是Object類型的實例,這就是為什么可以在其上使用in運算符的原因。

結論 (Conclusion)

You’ve learned about the not so popular JavaScript in operator, which is used to verify the presence of properties on an object or Object type instances. This should come in handy when writing verification logic.

您已經了解了不太流行JavaScript in運算符,該運算符用于驗證對象或Object類型實例上屬性的存在。 在編寫驗證邏輯時,這應該派上用場。

If you liked this article, you’ll definitely like other articles on my blog codewithlinda.com. There I publish beginner friendly articles on frontend development sans technical jargon (as much as possible) 😁.

如果您喜歡這篇文章,那么您肯定會喜歡我的博客codewithlinda.com上的其他文章。 我在那里發表有關前端開發的初學者友好文章,沒有技術術語(盡可能)。

翻譯自: https://www.freecodecamp.org/news/the-javascript-in-operator-explained-with-examples/

sql語句中的in用法示例

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

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

相關文章

vue項目實戰總結

馬上過年了,最近工作不太忙,再加上本人最近比較懶,毫無斗志,不愿學習新東西,或許是要過年的緣故(感覺像是在找接口)。 就把前一段時間做過的vue項目,進行一次完整的總結。 這次算是詳細總結,會從…

Linux !的使用

轉自:https://www.linuxidc.com/Linux/2015-05/117774.htm 一、history    78 cd /mnt/ 79 ls 80 cd / 81 history 82 ls 83 ls /mnt/ !78 相當于執行cd /mnt !-6 也相當于執行cd /mnt 二、!$ cd /mnt ls !$ 相當于執行 ls /mnt轉載于:https://www.cnblogs.…

881. 救生艇

881. 救生艇 第 i 個人的體重為 people[i],每艘船可以承載的最大重量為 limit。 每艘船最多可同時載兩人,但條件是這些人的重量之和最多為 limit。 返回載到每一個人所需的最小船數。(保證每個人都能被船載)。 示例 1: 輸入:…

使用python數據分析_如何使用Python提升您的數據分析技能

使用python數據分析If youre learning Python, youve likely heard about sci-kit-learn, NumPy and Pandas. And these are all important libraries to learn. But there is more to them than you might initially realize.如果您正在學習Python,則可能聽說過sci…

openresty 日志輸出的處理

最近出了個故障,有個接口的請求居然出現了長達幾十秒的處理時間,由于日志缺乏,網絡故障也解除了,就沒法再重現這個故障了。為了可以在下次出現問題的時候能追查到問題,所以需要添加一些追蹤日志。添加這些追蹤日志&…

誰是贏家_贏家的真正作品是股東

誰是贏家As I wrote in the article “5 Skills to Look For When Hiring Remote Talent,” remote work is a fast emerging segment of the labor market. Today roughly eight million Americans work remotely full-time. And among the most commonly held jobs include m…

博客園代碼黑色主題高亮設置

參考鏈接: https://segmentfault.com/a/1190000013001367 先發鏈接,有空實踐后會整理。我的GitHub地址:https://github.com/heizemingjun我的博客園地址:http://www.cnblogs.com/chenmingjun我的螞蟻筆記博客地址:http…

Matplotlib課程–學習Python數據可視化

Learn the basics of Matplotlib in this crash course tutorial. Matplotlib is an amazing data visualization library for Python. You will also learn how to apply Matplotlib to real-world problems.在此速成班教程中學習Matplotlib的基礎知識。 Matplotlib是一個很棒…

Android 開發使用 Gradle 配置構建庫模塊的工作方式

Android 開發過程中,我們不可避免地需要引入其他人的工作成果。減少重復“造輪子”的時間,投入到更有意義的核心任務當中。Android 庫模塊在結構上與 Android 應用模塊相同。提供構建應用所需的一切內容,包括源代碼(src&#xff0…

vue 組件庫發布_如何創建和發布Vue組件庫

vue 組件庫發布Component libraries are all the rage these days. They make it easy to maintain a consistent look and feel across an application. 如今,組件庫風行一時。 它們使在整個應用程序中保持一致的外觀和感覺變得容易。 Ive used a variety of diff…

angular

<input type"file" id"one-input" accept"image/*" file-model"images" οnchange"angular.element(this).scope().img_upload(this.files)"/>轉載于:https://www.cnblogs.com/loweringye/p/8441437.html

Java網絡編程 — Netty入門

認識Netty Netty簡介 Netty is an asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients. Netty is a NIO client server framework which enables quick and easy development o…

har文件分析http_如何使用HAR文件分析一段時間內的性能

har文件分析httpWhen I consider the performance of a website, several things come to mind. I think about looking at the requests of a page, understanding what resources are being loaded, and how long these resources take to be available to users.當我考慮網站…

第一階段:前端開發_Mysql——表與表之間的關系

2018-06-26 表與表之間的關系 一、一對多關系&#xff1a; 常見實例&#xff1a;分類和商品&#xff0c;部門和員工一對多建表原則&#xff1a;在從表&#xff08;多方&#xff09;創建一個字段&#xff0c;字段作為外鍵指向主表&#xff08;一方&#xff09;的一方      …

按鈕提交在url后添加字段_在輸入字段上定向單擊“清除”按鈕(X)

按鈕提交在url后添加字段jQuery makes it easy to get your project up and running. Though its fallen out of favor in recent years, its still worth learning the basics, especially if you want quick access to its powerful methods.jQuery使您可以輕松啟動和運行項目…

429. N 叉樹的層序遍歷

429. N 叉樹的層序遍歷 給定一個 N 叉樹&#xff0c;返回其節點值的層序遍歷。&#xff08;即從左到右&#xff0c;逐層遍歷&#xff09;。 樹的序列化輸入是用層序遍歷&#xff0c;每組子節點都由 null 值分隔&#xff08;參見示例&#xff09;。 - 示例 1&#xff1a;輸入…

javascript如何阻止事件冒泡和默認行為

阻止冒泡&#xff1a; 冒泡簡單的舉例來說&#xff0c;兒子知道了一個秘密消息&#xff0c;它告訴了爸爸&#xff0c;爸爸知道了又告訴了爺爺&#xff0c;一級級傳遞從而以引起事件的混亂&#xff0c;而阻止冒泡就是不讓兒子告訴爸爸&#xff0c;爸爸自然不會告訴爺爺。下面的d…

89. Gray Code - LeetCode

為什么80%的碼農都做不了架構師&#xff1f;>>> Question 89. Gray Code Solution 思路&#xff1a; n 0 0 n 1 0 1 n 2 00 01 10 11 n 3 000 001 010 011 100 101 110 111 Java實現&#xff1a; public List<Integer> grayCode(int n) {List&…

400. 第 N 位數字

400. 第 N 位數字 在無限的整數序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …中找到第 n 位數字。 注意&#xff1a;n 是正數且在 32 位整數范圍內&#xff08;n < 231&#xff09;。 示例 1&#xff1a; 輸入&#xff1a;3 輸出&#xff1a;3 示例 2&#xff1a; 輸入&…

1.初識Linux

1.Linux 區分大小寫 2.shell命令行-bash 進入終端->[stulocalhost~]$ (其中,Stu為登錄用戶名&#xff0c;localhost為登錄主機名&#xff0c;’~’ 表示當前用戶正處在stu用戶的家目錄中, 普通用戶的提示符以$結尾&#xff0c;而根用戶以’#’結尾) 3.Linux中所謂的命令(…