What's the difference between tilde(~) and caret(^) in package.json?

After I upgraded to latest stable node and npm , I tried npm install moment --save . It saves the entry in the package.json with the caret(^) prefix. Previously, it was a tilde(~) prefix. Why are these changes made in npm ? What is the difference between tilde(~) and caret(^) ? What is the advantages over others? In the simplest terms, the tilde matches the most recent minor version (th

package.json中tilde(〜)和caret(^)之间有什么区别?

在升级到最新的稳定node和npm ,我尝试了npm install moment --save 。 它用caret(^)前缀保存package.json的条目。 以前,它是一个tilde(~)前缀。 为什么在npm进行这些更改? tilde(~)和caret(^)之间的区别是什么? 与其他人相比,有什么优势? 用最简单的术语来说,波形符与最新的次要版本(中间数字)匹配。 〜1.2.3将匹配所有1.2.x版本,但会错过1.3.0。 另一方面,插入符号更轻松。 它会将您更新到最新的主要

Check if object is array?

I'm trying to write a function that either accepts a list of strings, or a single string. If it's a string, then I want to convert it to an array with just the one item. Then I can loop over it without fear of an error. So how do I check if the variable is an array? I've rounded up the various solutions below and created a jsperf test. In modern browsers you can do Array.isAr

检查对象是否是数组?

我试图编写一个接受一个字符串列表或一个字符串的函数。 如果它是一个字符串,那么我想将它转换为只有一个项目的数组。 然后我可以循环使用,而不用担心错误。 那么如何检查变量是否是一个数组? 我已经围绕下面的各种解决方案,并创建了一个jsperf测试。 在现代浏览器中你可以做到 Array.isArray(obj) (由Chrome 5,Firefox 4.0,IE 9,Opera 10.5和Safari 5支持) 为了向后兼容,您可以添加以下内容 # only imple

.prop() vs .attr()

So jQuery 1.6 has the new function prop() . $(selector).click(function(){ //instead of: this.getAttribute('style'); //do i use: $(this).prop('style'); //or: $(this).attr('style'); }) or in this case do they do the same thing? And if I do have to switch to using prop() , all the old attr() calls will break if i switch to 1.6? UPDATE selector = '#id' $(selector).cl

.prop()vs .attr()

所以jQuery 1.6有了新的函数prop() 。 $(selector).click(function(){ //instead of: this.getAttribute('style'); //do i use: $(this).prop('style'); //or: $(this).attr('style'); }) 或者在这种情况下,他们做同样的事情吗? 如果我必须切换到使用prop() ,如果我切换到1.6,所有旧的attr()调用都会中断吗? UPDATE selector = '#id' $(selector).click(function() { //instead of:

How do you check for an empty string in JavaScript?

I saw this thread, but I didn't see a JavaScript specific example. Is there a simple string.Empty available in JavaScript, or is it just a case of checking for "" ? If you just want to check whether there's any value, you can do if (strValue) { //do something } If you need to check specifically for an empty string over null, I would think checking against "" i

你如何检查JavaScript中的空字符串?

我看到了这个线程,但我没有看到一个JavaScript特定的例子。 有没有简单的string.Empty在JavaScript中可用,还是仅仅是检查"" ? 如果你只是想检查是否有任何价值,你可以做 if (strValue) { //do something } 如果你需要特别检查空字符串是否超过null,我会认为使用===运算符来检查""是最好的选择(这样你就知道它实际上是一个你正在比较的字符串)。 为了检查一个字符串是否为空,null或undefi

How to list the properties of a JavaScript object?

Say I create an object thus: var myObject = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}; What is the best way to retrieve a list of the property names? ie I would like to end up with some variable 'keys' such that: keys == ["ircEvent", "method", "regex"] In modern browsers (IE9+, FF4+, Chrome5+, Opera12+, Safari5+) you can use the built in Object.keys m

如何列出JavaScript对象的属性?

假设我创建了一个对象: var myObject = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}; 检索属性名称列表的最佳方法是什么? 即,我想最终得到一些变量“键”,使得: keys == ["ircEvent", "method", "regex"] 在现代浏览器(IE9 +,FF4 +,Chrome5 +,Opera12 +,Safari5 +)中,您可以使用内置的Object.keys方法: var keys = Object.keys(myObject); 上面有一个完整的polyfill,但是一

How can I merge properties of two JavaScript objects dynamically?

I need to be able to merge two (very simple) JavaScript objects at runtime. For example I'd like to: var obj1 = { food: 'pizza', car: 'ford' } var obj2 = { animal: 'dog' } obj1.merge(obj2); //obj1 now has three properties: food, car, and animal Does anyone have a script for this or know of a built in way to do this? I do not need recursion, and I do not need to merge functions, just met

我如何动态合并两个JavaScript对象的属性?

我需要能够在运行时合并两个(非常简单的)JavaScript对象。 例如,我想: var obj1 = { food: 'pizza', car: 'ford' } var obj2 = { animal: 'dog' } obj1.merge(obj2); //obj1 now has three properties: food, car, and animal 有没有人有这个脚本或知道内置的方式来做到这一点? 我不需要递归,也不需要合并函数,只需要平面对象上的方法。 ECMAScript 2018标准方法 你会使用对象休息传播: let merged = {...obj1

How do I check if an object has a property in JavaScript?

How do I check if an object has a property in JavaScript? Consider: x = {'key': 1}; if ( x.hasOwnProperty('key') ) { //Do this } Is that the best way to do it? I'm really confused by the answers that have been given - most of them are just outright incorrect. Of course you can have object properties that have undefined, null, or false values. So simply reducing the property check

如何检查一个对象是否在JavaScript中有属性?

如何检查一个对象是否在JavaScript中有属性? 考虑: x = {'key': 1}; if ( x.hasOwnProperty('key') ) { //Do this } 这是做这件事的最好方法吗? 我对已经给出的答案感到困惑 - 他们中的大多数完全是错误的。 当然,您可以拥有未定义,空值或假值的对象属性。 所以简单地减少属性检查来typeof this[property]或者更糟的是, x.key会给你带来完全误导的结果。 这取决于你在找什么。 如果你想知道一个对象是否物

Length of a JavaScript object

If I have a JavaScript object, say var myObject = new Object(); myObject["firstname"] = "Gareth"; myObject["lastname"] = "Simpson"; myObject["age"] = 21; is there a built-in or accepted best practice way to get the length of this object? The most robust answer (ie that captures the intent of what you're trying to do while causing the fewest bugs) would be: Object.size = function(obj) {

JavaScript对象的长度

如果我有一个JavaScript对象,说 var myObject = new Object(); myObject["firstname"] = "Gareth"; myObject["lastname"] = "Simpson"; myObject["age"] = 21; 有没有一种内置的或被接受的最佳实践方式来获得这个对象的长度? 最强大的答案(即捕捉你想要做的事情的意图,同时导致最少的错误)将是: Object.size = function(obj) { var size = 0, key; for (key in obj) { if (obj.hasOwnProperty(key)) s

What is JavaScript garbage collection?

What is JavaScript garbage collection? What's important for a web programmer to understand about JavaScript garbage collection, in order to write better code? Eric Lippert wrote a detailed blog post about this subject a while back (additionally comparing it to VBScript). More accurately, he wrote about JScript, which is Microsoft's own implementation of ECMAScript, although very simil

什么是JavaScript垃圾收集?

什么是JavaScript垃圾收集? 为了编写更好的代码,对于Web程序员来说,了解JavaScript垃圾收集有什么重要意义? Eric Lippert写了一篇关于此主题的详细博客文章 (另外还将其与VBScript比较)。 更准确地说,他写了关于JScript的文章,这是微软自己实现的ECMAScript,尽管与JavaScript很相似。 我想你可以认为绝大多数的行为对于Internet Explorer的JavaScript引擎是一样的。 当然,从浏览器到浏览器的实现会有所不同,但

How to determine whether an object has a given property in JavaScript

How can I determine whether an object x has a defined property y , regardless of the value of xy ? I'm currently using if (typeof(x.y) !== 'undefined') but that seems a bit clunky. Is there a better way? Object has property: If you are testing for properties that are on the object itself (not a part of its prototype chain) you can use .hasOwnProperty() : if (x.hasOwnProperty('y')) {

如何确定一个对象是否在JavaScript中有一个给定的属性

无论xy的值如何,我如何确定对象x是否具有已定义的属性y ? 我目前正在使用 if (typeof(x.y) !== 'undefined') 但这似乎有点笨重。 有没有更好的办法? 对象有属性: 如果您正在测试对象本身的属性(不是其原型链的一部分),则可以使用.hasOwnProperty() : if (x.hasOwnProperty('y')) { // ...... } 对象或其原​​型有一个属性: 您可以使用in运算符来测试继承的属性。 if ('y' in x) { // ...... } 如果