js array contains All In One
includes & contains & has
Array.prototype.contains
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-10-01
* @modified
*
* @description Array.prototype.contains
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
* @best_solutions
*
*/
const log = console.log;
Array.prototype.contains = function (key) {
// log(`
this`, this, this.length);
// for (let item in this) {
for (let item of this) {
// log(`item =,`, item);
// log(`key =`, key);
// if (item == key) {
// if (item === key) {
// type checker Object.prototype.toString()
// log(`JSON.stringify(item) === JSON.stringify(key)`, JSON.stringify(item) , JSON.stringify(key));
if (JSON.stringify(item) === JSON.stringify(key)) {
// break;
return true;
} else {
// continue;
}
}
return false;
}
const arr = [1, "3", {}];
const test = arr.contains("abc");
const test1 = arr.contains("3");
const test2 = arr.contains({});
log(`test =`, test)
log(`test1 =`, test1)
log(`test2 =`, test2)
/*
test = false
test1 = true
test2 = false
*/
/*
test = false
test1 = true
test2 = true
*/
覆盖 prototype 方法
扩展 prototype 方法
contains
https://developer.mozilla.org/en-US/docs/Web/API/Node/contains
https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList/contains
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/permissions/contains
css contain
https://developer.mozilla.org/en-US/docs/Web/CSS/contain
/* Keyword values */
contain: none;
contain: strict;
contain: content;
contain: size;
contain: layout;
contain: style;
contain: paint;
/* Multiple keywords */
contain: size paint;
contain: size layout paint;
/* Global values */
contain: inherit;
contain: initial;
contain: unset;
布局优化,性能优化, 重绘重排
https://drafts.csswg.org/css-contain-1/
hasOwnProperty
has
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
refs
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
https://www.w3schools.com/jsref/jsref_includes_array.asp#:~:text=The includes() method determines,()%20method%20is%20case%20sensitive.
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!