What is the most efficient way to deep clone an object in JavaScript?

What is the most efficient way to clone a JavaScript object? I've seen obj = eval(uneval(o)); being used, but that's non-standard and only supported by Firefox. I've done things like obj = JSON.parse(JSON.stringify(o)); but question the efficiency. I've also seen recursive copying functions with various flaws. I'm surprised no canonical solution exists. Note: This i

在JavaScript中深入克隆对象的最有效方法是什么?

什么是克隆JavaScript对象最有效的方法? 我见过obj = eval(uneval(o)); 正在使用,但这是非标准的,只有Firefox支持。 我做了像obj = JSON.parse(JSON.stringify(o)); 但质疑效率。 我也看到了带有各种缺陷的递归复制函数。 我很惊讶没有规范的解决方案存在。 注意:这是对另一个答案的回复,而不是对此问题的正确答复。 如果您希望快速克隆对象,请按照Corban的建议回答这个问题。 我想说明的是, jQuery中的.cl

How do I remove a property from a JavaScript object?

Say I create an object as follows: var myObject = { "ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*" }; What is the best way to remove the property regex to end up with new myObject as follows? var myObject = { "ircEvent": "PRIVMSG", "method": "newURI" }; Like this: delete myObject.regex; // or, delete myObject['regex']; // or, var prop = "regex"; delete m

我如何从JavaScript对象中移除一个属性?

假设我创建一个对象如下: var myObject = { "ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*" }; 如何删除属性regex最终以new myObject结束的最佳方式如下? var myObject = { "ircEvent": "PRIVMSG", "method": "newURI" }; 喜欢这个: delete myObject.regex; // or, delete myObject['regex']; // or, var prop = "regex"; delete myObject[prop]; 演示 var myObject = {

"Thinking in AngularJS" if I have a jQuery background?

Suppose I'm familiar with developing client-side applications in jQuery, but now I'd like to start using AngularJS. Can you describe the paradigm shift that is necessary? Here are a few questions that might help you frame an answer: How do I architect and design client-side web applications differently? What is the biggest difference? What should I stop doing/using; What should I

“在AngularJS中思考”如果我有一个jQuery背景?

假设我熟悉在jQuery中开发客户端应用程序,但现在我想开始使用AngularJS。 你能描述一下必要的范式转变吗? 这里有几个问题可以帮助你构建一个答案: 我如何构建和设计不同的客户端Web应用程序? 最大的区别是什么? 我应该停止做什么/使用什么; 我应该开始做什么/使用什么? 有没有服务器端考虑/限制? 我没有在jQuery和AngularJS之间寻找详细的比较。 1.不要设计你的页面,然后用DOM操作来改变它 在jQuery中,

Which equals operator (== vs ===) should be used in JavaScript comparisons?

I'm using JSLint to go through JavaScript, and it's returning many suggestions to replace == (two equals signs) with === (three equals signs) when doing things like comparing idSele_UNVEHtype.value.length == 0 inside of an if statement. Is there a performance benefit to replacing == with === ? Any performance improvement would be welcomed as many comparison operators exist. If no ty

应该在JavaScript比较中使用哪个等于运算符(== vs ===)?

我使用JSLint来浏览JavaScript,并且它返回了很多建议,以便在比较idSele_UNVEHtype.value.length == 0时使用=== (三个等号)替换== (两个等号) if声明。 用===替换==是否有性能优势? 许多比较运算符存在,任何性能改进都会受到欢迎。 如果没有类型转换发生,会有比==更好的性能吗? 身份( === )运算符的行为与相等( == )运算符的行为相同,除非没有进行类型转换,并且类型必须相同才能被视为相等。 参考:Jav

How do I remove a particular element from an array in JavaScript?

I have an array of integers, and I'm using the .push() method to add elements to it. Is there a simple way to remove a specific element from an array? The equivalent of something like array.remove(int); . I have to use core JavaScript - no frameworks are allowed. Find the index of the array element you want to remove, then remove that index with splice . var array = [2, 5, 9]; var in

如何从JavaScript中的数组中删除特定的元素?

我有一个整数数组,我使用.push()方法向它添加元素。 有没有简单的方法从数组中删除特定的元素? 相当于类似array.remove(int); 。 我必须使用核心JavaScript - 不允许任何框架。 找到要删除的数组元素的index ,然后使用splice删除该索引。 var array = [2, 5, 9]; var index = array.indexOf(5); if (index > -1) { array.splice(index, 1); } // array = [2, 9] splice的第二个参数是要移除的元素的数量。

var functionName = function() {} vs function functionName() {}

I've recently started maintaining someone else's JavaScript code. I'm fixing bugs, adding features and also trying to tidy up the code and make it more consistent. The previous developer uses two ways of declaring functions and I can't work out if there is a reason behind it or not. The two ways are: var functionOne = function() { // Some code }; function functionTwo() {

var functionName = function(){} vs function functionName(){}

我最近开始维护别人的JavaScript代码。 我正在修复错误,增加功能,并试图整理代码并使其更加一致。 前面的开发人员使用两种声明函数的方法,如果有或没有原因,我无法解决。 两种方法是: var functionOne = function() { // Some code }; function functionTwo() { // Some code } 使用这两种不同方法的原因是什么?每种方法的优缺点是什么? 有一种方法可以用另一种方法完成吗? 所不同的是, functionOne

How do I check if an element is hidden in jQuery?

It is possible to toggle the visibility of an element, using the functions .hide() , .show() or .toggle() . How would you test if an element is visible or hidden? Since the question refers to a single element, this code might be more suitable: // Checks css for display:[none|block], ignores visibility:[true|false] $(element).is(":visible"); Same as twernt's suggestion, but applied to a

我如何检查一个元素是否隐藏在jQuery中?

是可能的切换元件的可见性,使用函数.hide() .show()或.toggle() 你会如何测试一个元素是否可见或隐藏? 由于该问题涉及单个元素,因此此代码可能更合适: // Checks css for display:[none|block], ignores visibility:[true|false] $(element).is(":visible"); 与twernt的建议相同,但适用于单个元素; 它与jQuery FAQ中推荐的算法相匹配 您可以使用hidden选择器: // Matches all elements that are hidden $('eleme

How to check whether a string contains a substring in JavaScript?

Usually I would expect a String.contains() method, but there doesn't seem to be one. What is a reasonable way to check for this? Here is a list of current possibilities: 1. (ES6) includes —go to answer var string = "foo", substring = "oo"; string.includes(substring); 2. ES5 and older indexOf var string = "foo", substring = "oo"; string.indexOf(substring) !== -1; String.proto

如何检查一个字符串是否包含JavaScript中的子字符串?

通常我会期望一个String.contains()方法,但似乎并不存在。 什么是合理的方法来检查这个? 这里列出了当前的可能性: 1.(ES6) includes要回答 var string = "foo", substring = "oo"; string.includes(substring); 2. ES5和更老的indexOf var string = "foo", substring = "oo"; string.indexOf(substring) !== -1; String.prototype.indexOf返回另一个字符串中字符串的位置。 如果未找到,它将返回-1 。

How do I redirect to another webpage?

如何使用jQuery或纯JavaScript将用户从一个页面重定向到另一个页面? One does not simply redirect using jQuery jQuery is not necessary, and window.location.replace(...) will best simulate an HTTP redirect. window.location.replace(...) is better than using window.location.href , because replace() does not keep the originating page in the session history, meaning the user won't get stuck in a n

我如何重定向到另一个网页?

如何使用jQuery或纯JavaScript将用户从一个页面重定向到另一个页面? 一个不会简单地使用jQuery重定向 jQuery不是必需的,并且window.location.replace(...)将最好地模拟HTTP重定向。 window.location.replace(...)比使用window.location.href要好,因为replace()不会将原始页面保留在会话历史记录中,这意味着用户不会陷入永无止境的后退,按钮失败。 如果你想模拟某人点击链接,请使用location.href 如果您想模拟HTTP

How do JavaScript closures work?

How would you explain JavaScript closures to someone with a knowledge of the concepts they consist of (for example functions, variables and the like), but does not understand closures themselves? I have seen the Scheme example given on Wikipedia, but unfortunately it did not help. JavaScript closures for beginners Submitted by Morris on Tue, 2006-02-21 10:19. Community-edited since. Clos

JavaScript关闭如何工作?

你如何将JavaScript闭包解释给懂得它们构成的概念的人(例如函数,变量等),但不明白闭包本身? 我已经看到维基百科给出的Scheme示例,但不幸的是它没有帮助。 适合初学者的JavaScript闭包 由Morris在星期二提交,2006-02-21 10:19。 自社区编辑。 关闭不是魔术 本页面介绍了闭包,以便程序员能够理解它们 - 使用正在运行的JavaScript代码。 它不适用于大师或功能性程序员。 一旦核心概念得到修复,关闭并不难理