


Note: If there is any inappropriateness in the translation, please correct me. I wish you all a happy Double Holidays!
It is not good to extend native objects and prototypes without special needs
//Don’t do this
Array.prototype.map = function() {
// Some code
};
Unless it's worthwhile to do so, e.g. add some ECMAScript5 methods to some older browsers.
In this case, we generally do this:
if (!Array.prototype.map) {
Array.prototype.map = function() {
//Some code
};
}
If we are more paranoid, in order to prevent others from defining map as other unexpected values, such as true or others, we can change the detection code to the following:
if (typeof Array.prototype.map !== "function") {
Array.prototype.map = function() {
// Some code
};
}
(Although this will break other developers’ map definitions and affect the implementation of their functions)
However, in In a hostile and competitive environment (in other words, when you provide or use a js library), you should not trust anyone. What should you do if someone else's js code is loaded before yours and somehow defines a map() method that is not fully compatible with ES5, causing your code to not run properly?
However, you can trust the browser. If the Webkit kernel implements the map() method, you can rest assured that this method will definitely run normally. Otherwise, you have to use your code to detect it.
Fortunately, this is easy to implement in JavaScript. When you call the toString method of a native function, a string of a function will be returned. The function body of the function is [native code].
For example, under the Chrome console:
> ; Array.prototype.map.toString();
"function map() { [native code] }"
A proper code inspection is always a slightly unpleasant thing, Because different browsers treat spaces and line breaks too lightly. The test is as follows:
Array.prototype.map.toString() .replace(/s/g, '*');
// "*function*map()*{*****[native*code]*}*" // IE
// " function*map()*{*****[native*code]*}" // FF
// "function*map()*{*[native*code]*}" // Chrome
Simply removing the s will result in a more practical string:
Array.prototype.map.toString().replace(/s/g, '');
// "functionmap(){[nativecode]}"
You can encapsulate it into a reusable shim() function, so that you don’t have to repeat all the operations like!
Array.prototype... Such operations. This function will accept an object as a parameter (for example, Array.prototype), a property to be added (for example, 'map'), and a function to be added.
function shim(o, prop, fn) {
var nbody = "function" prop "(){[nativecode]}";
if (o.hasOwnProperty(prop) &&
o[prop].toString().replace(/s/g, ' ') === nbody) {
//The table name is native!
return true;
}
//Newly added
o[prop] = fn;
}
Test:
//This is the native method
shim(
Array.prototype, 'map',
function(){/*...*/}
); / / true
//This is the newly added method
shim(
Array.prototype, 'mapzer',
function(){alert(this)}
);
[ 1,2,3].mapzer(); // alerts 1,2,3
(End)^_^

原神4.4版本新地图介绍,小伙伴们原神这次4.4版本也是迎来了璃月的海灯节,同时将在4.4版本推出一个新的地图区域,名为沉玉谷。根据提供的信息,沉玉谷实际上是翘英庄的一部分,但玩家更习惯称其为沉玉谷。下面就让小编来给大家介绍一下新地图吧。原神4.4版本新地图介绍4.4版本将开放璃月北部的「沉玉谷·上谷」、「沉玉谷·南陵」和「来歆山」,在「沉玉谷·上谷」已为旅行者开启传送锚点。※完成魔神任务序章·第三幕巨龙与自由之歌」后,将自动解锁该传送锚点。二、翘英庄当春日温煦的柔风再度抚过沉玉的山野,那馥郁的

原型,js中的一个对象,用于定义其他对象的属性和方法,每个构造函数都有一个prototype属性,这个属性是一个指针,指向一个原型对象,当创建新对象时,这个新对象会从其构造函数的prototype属性继承属性和方法。原型链,当试图访问一个对象的属性时,js会首先检查这个对象是否有这个属性,如果没有,那么js会转向这个对象的原型,如果原型对象也没有这个属性,会继续查找原型的原型。

Go语言和Python是两种非常流行的编程语言,都具有各自的优势和特点。在高性能编程方面,两者也有一些不同之处。本文将对Go语言和Python进行比较,以探讨哪个更适用于高性能编程。首先,让我们来了解一下Go语言。Go语言是由谷歌公司开发的一种开源编程语言,它专注于简洁、高效和并发性。Go语言的设计目标之一是提供高性能的编程体验。它具备轻量级的协程(goro

原型和原型链的区别是:1、原型是每个对象都具有的属性,包含了一些共享的属性和方法,用于实现对象之间的属性和方法的共享和继承,而原型链是一种通过对象之间的原型关系来实现继承的机制,定义了对象之间的继承关系,使得对象可以共享原型对象的属性和方法;2、原型的作用是定义对象的共享属性和方法,使得多个对象可以共享同一个原型对象的属性和方法,而原型链的作用是实现对象之间的继承关系等等。

在当今科技进步迅猛的时代,编程语言的选择变得非常关键。随着软件开发领域的不断发展,Go语言和Python成为了两个备受关注的编程语言。本文将对Go语言和Python进行对比分析,以帮助读者根据项目需求选择合适的编程语言。首先,让我们来了解一下Go语言。Go语言是由Google公司开发的一种静态编译型编程语言。它具有强大的并发处理能力和高效的垃圾回收机制,非常

在过去一年中,随着大模型技术的广泛应用,我们已经见证了AI如何深刻地改变着我们的工作方式。在程序编写领域,AI的介入同样将为程序员们带来前所未有的便利。近日,非十科技推出了一款基于自研代码大模型打造的AI代码助手——FittenCode,它可以帮助程序员更迅捷、更准确、更高质量地完成编码任务,大幅提升编码效率,并且向用户免费开放使用!产品官网地址:https://code.fittentech.com/FittenCode自上次发布以来迅速走红。开发团队日以继夜地工作,带来了功能、

js原型和原型链的作用是实现对象的继承,节省内存空间,并提高代码的性能和可维护性。详细介绍:1、实现对象的继承,原型和原型链允许创建一个对象,并使其继承另一个对象的属性和方法,当创建一个新的对象时,可以将其原型指向另一个对象,这样新对象就可以访问到原型对象上的属性和方法;2、节省内存和提高性能,在JavaScript中,每个对象都有一个原型,通过原型链,对象可以共享原型上等等。

原型的特点是:1、原型是一个普通的对象,它可以拥有属性和方法,就像任何其他对象一样;2、在创建对象时,会自动关联一个原型。当我们创建一个新对象时,JavaScript将自动为该对象分配一个原型,并将其与对象相关联;3、对象可以通过原型链访问原型的属性和方法;原型链的特点是:1、每个对象都有一个原型,当访问一个对象的属性时,如果该对象本身没有该属性,则会在原型对象中查找等等。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
