Generate random string/characters in JavaScript

I want a 5 character string composed of characters picked randomly from the set [a-zA-Z0-9] . What's the best way to do this with JavaScript? 我认为这会对你有用: function makeid() { var text = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for (var i = 0; i < 5; i++) text += possible.charAt(Math.floor(Math.random() * possible.length

在JavaScript中生成随机字符串/字符

我想要一个由字符串[a-zA-Z0-9]随机选取的5个字符的字符串。 用JavaScript做这件事的最好方法是什么? 我认为这会对你有用: function makeid() { var text = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for (var i = 0; i < 5; i++) text += possible.charAt(Math.floor(Math.random() * possible.length)); return text; } console.log(makeid());

How do I convert a string into an integer in JavaScript?

如何在JavaScript中将string转换为integer ? The simplest way would be to use the native Number function: var x = Number("1000") If that doesn't work for you, then there are the parseInt , unary plus , parseFloat with floor , and Math.round methods. parseInt: var x = parseInt("1000", 10); // you want to use radix 10 // so you get a decimal number even with a leading 0 and an old browse

如何在JavaScript中将字符串转换为整数?

如何在JavaScript中将string转换为integer ? 最简单的方法是使用本机Number函数: var x = Number("1000") 如果这不适合你,那么就有parseInt , unary plus , parseFloat和floor ,以及Math.round方法。 parseInt函数: var x = parseInt("1000", 10); // you want to use radix 10 // so you get a decimal number even with a leading 0 and an old browser ([IE8, Firefox 20, Chrome 22 and older][1]) 一元加

Deleting array elements in JavaScript

What is the difference between using the delete operator on the array element as opposed to using the Array.splice method? For example: myArray = ['a', 'b', 'c', 'd']; delete myArray[1]; // or myArray.splice (1, 1); Why even have the splice method if I can delete array elements like I can with objects? delete will delete the object property, but will not reindex the array or update its le

在JavaScript中删除数组元素

在数组元素上使用delete操作符与使用Array.splice方法相比有什么Array.splice ? 例如: myArray = ['a', 'b', 'c', 'd']; delete myArray[1]; // or myArray.splice (1, 1); 为什么即使有splice方法,如果我可以用对象删除数组元素? delete将删除对象属性,但不会重新索引数组或更新其长度。 这使得它看起来好像是未定义的: > myArray = ['a', 'b', 'c', 'd'] ["a", "b", "c", "d"] > delete myArray[0] t

decodeURIComponent vs unescape, what is wrong with unescape?

In answering another question I became aware that my Javascript/DOM knowledge had become a bit out of date in that I am still using escape / unescape to encode the contents of URL components whereas it appears I should now be using encodeURIComponent / decodeURIComponent instead. What I want to know is what is wrong with escape / unescape ? There are some vague suggestions that there is some s

decodeURIComponent vs unescape,unescape有什么问题?

在回答另一个问题时,我意识到我的Javascript / DOM知识已经过时了,因为我仍然使用escape / unescape来编码URL组件的内容,而现在我应该使用encodeURIComponent / decodeURIComponent 。 我想知道的是escape / unescape有什么问题? 有一些模糊的建议,说Unicode字符存在某种问题,但我找不到任何明确的解释。 我的网站体验相当有偏见,几乎所有的网站都在编写与Internet Explorer绑定的大型Intranet应用程序。 这涉及到

JavaScript query string

Is there any JavaScript library that makes a dictionary out of the query string, ASP.NET style? Something which can be used like: var query = window.location.querystring["query"]? Is "query string" called something else outside the .NET realm? Why isn't location.search broken into a key/value collection ? EDIT : I have written my own function, but does any major JavaScript li

JavaScript查询字符串

有没有任何JavaScript库使查询字符串, ASP.NET风格的字典? 可以使用的东西,如: var query = window.location.querystring["query"]? “查询字符串”在.NET领域之外被称为别的东西吗? 为什么不将location.search分解为关键/值集合? 编辑 :我写了我自己的功能,但是没有任何主要的JavaScript库吗? 也许http://plugins.jquery.com/query-object/? 这是它的分支https://github.com/sousk/jquery.parsequery#readme

In Node.js, how do I "include" functions from my other files?

Let's say I have a file called app.js. Pretty simple: var express = require('express'); var app = express.createServer(); app.set('views', __dirname + '/views'); app.set('view engine', 'ejs'); app.get('/', function(req, res){ res.render('index', {locals: { title: 'NowJS + Express Example' }}); }); app.listen(8080); What if I have a functions inside "tools.js". How would

在Node.js中,我如何从其他文件“包含”功能?

假设我有一个名为app.js的文件。 很简单: var express = require('express'); var app = express.createServer(); app.set('views', __dirname + '/views'); app.set('view engine', 'ejs'); app.get('/', function(req, res){ res.render('index', {locals: { title: 'NowJS + Express Example' }}); }); app.listen(8080); 如果我在“tools.js”中有一个函数,该怎么办? 我将如何导入它们以在apps.js中使用?

check to see if string matches requirements

The function generateHashtag(str); is passed a string that must meet the following criteria: If the final result is longer than 140 chars it must return false. If the input is a empty string it must return false. It must start with a hashtag (#). All words must have their first letter capitalized. Example Input to Output: " Hello there thanks for trying my Kata" => "

检查字符串是否符合要求

函数generateHashtag(str); 传递一个必须符合以下标准的字符串: 如果最终结果超过140个字符,它必须返回false。 如果输入是一个空字符串,它必须返回false。 它必须以标签(#)开头。 所有的单词必须首字母大写。 输入输出示例: “你好,谢谢你尝试我的Kata”=>“#HelloThereThanksForTryingMyKata” “Hello World”=>“#HelloWorld” 这是我的代码到目前为止: function generateHashtag (str) { if (!s

compulsive binding of this with functions

I was just going through the code of jBox.js and came across the following lines of code: jQuery(document).on('keyup.jBox-' + this.id, function(ev) { (ev.keyCode == 37) && this.showImage('prev'); (ev.keyCode == 39) && this.showImage('next'); }.bind(this)); Have a look at the code HERE , this guy seems to compulsively bind functions with the value of this , I have a few q

这与功能的强制性绑定

我只是浏览了jBox.js的代码,并且遇到了以下几行代码: jQuery(document).on('keyup.jBox-' + this.id, function(ev) { (ev.keyCode == 37) && this.showImage('prev'); (ev.keyCode == 39) && this.showImage('next'); }.bind(this)); 看看这里的代码,这个家伙似乎强制性地绑定了this功能的价值,我对这个模式有几个问题。 这是一种设计模式吗? 作者试图通过将大多数方法与this绑定来实现什么

Modify a function or evaluate a function from string

This is what I currently have: test(function(){ console.dir('testing'); }); // Input a function function test(fn){ console.dir(fn.toString().replace('dir','log')); // console: function (){console.log('testing');} // try to evaluate string back to a function eval(fn.toString().replace('dir','log')); } Can I modify something in a function (eg,'testing' to 'hello

修改函数或从字符串评估函数

这是我目前拥有的: test(function(){ console.dir('testing'); }); // Input a function function test(fn){ console.dir(fn.toString().replace('dir','log')); // console: function (){console.log('testing');} // try to evaluate string back to a function eval(fn.toString().replace('dir','log')); } 我可以修改某个函数中的某些东西吗(例如,'测试'到'hello world')

How and why does 'a'['toUpperCase']() in JavaScript work?

JavaScript keeps surprising me and this is another instance. I just came across some code which I did not understood at first. So I debugged it and came to this finding: alert('a'['toUpperCase']()); //alerts 'A' Now this must be obvious if toUpperCase() is defined as a member of string type, but it did not make sense to me initially. Anyway, does this work because toUpperCase is a member

JavaScript中的'a'['toUpperCase']()如何以及为什么工作?

JavaScript让我感到惊讶,这是另一个例子。 我刚刚遇到了一些我一开始就不明白的代码。 所以我调试它并得出这个结论: alert('a'['toUpperCase']()); //alerts 'A' 现在,如果toUpperCase()被定义为字符串类型的成员,那么这一点很明显,但最初对我来说没有意义。 无论如何, 这是否是因为toUpperCase是'a'的成员? 或者幕后有其他事情发生? 我正在阅读的代码具有如下功能: function callMethod(method) {