Tag: JavaScript

在TypeScript中将数字转换为string

在Typescript中,从数字到string的最好方法是什么(如果有的话)? var page_number:number = 3; window.location.hash = page_number; 在这种情况下,编译器会抛出错误: types“数字”不能分配到types“string” 因为location.hash是一个string。 window.location.hash = ""+page_number; //casting using "" literal window.location.hash = String(number); //casting creating using the String() function 那么哪种方法更好?

最简单的backbone.js例子

我正在创build一个简单的骨架示例来尝试学习,并且遇到了让我的视图呈现的问题。 我已经把它build立在Thomas Davis的教程上,但是看了很多其他的应用程序和教程。 我正在改变Davis的教程,不仅因为我想添加一个input框,而且因为基于骨干文档,我认为它需要更less的代码和不同的结构。 显然,因为我不能得到这个工作,我不知道什么是需要的,什么不是。 我的最终目标是在ul#friends-list li添加li标签中的名字,尽pipe我不认为el: 'body'会帮助我。 我究竟做错了什么? 谢谢你的帮助。 我的html: <!DOCTYPE HTML> <html> <head> <title>Tut</title> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script> <script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.4/underscore-min.js"></script> <script type="text/javascript" src="http://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"></script> </head> <body> <input type="text" placeholder="Enter friend's name" id="input" /> <button id="add-input">Add Friend</button> <ul id="friends-list"> </ul> <script type="text/javascript" src="test.js"></script> </body> </html> 我的test.js $(function() { Friend = Backbone.Model.extend(); //Create my model var […]

为什么JavaScript在if语句中接受逗号?

我偶然发现了一些JavaScript语法,它似乎应该会产生某种types的parsing错误,但是不会: if (true, true) {console.log('splendid')} else {console.log('horrid')} // splendid if (true, false) {console.log('splendid')} else {console.log('horrid')} // horrid 似乎只有最后一个expression式影响逻辑,尽pipe所有expression式都被执行: if (console.log('super'), true) {console.log('splendid')} // super splendid 任何人都知道为什么这是有效的JavaScript语法? 有没有实际的用途呢?

负面看法正则expression式

我想匹配以“.htm”结尾的所有string,除非它以“foo.htm”结尾。 我通常体面的正则expression式,但负面的lookaheads让我难住。 为什么这不工作? /(?!foo)\.htm$/i.test("/foo.htm"); // returns true. I want false. 我应该使用什么呢? 我想我需要一个“负面看”的expression(如果JavaScript支持这样的事情,我知道它不)。

$(window).width()vs $(document).width()之间的区别

jQuery中$(window).width() vs $(document).width()是什么? 窗口是否表示浏览器,文档是否表示html页面的主体? 我对么 ?

你如何获得textarea中的光标位置?

我有一个textarea,我想知道如果我在textarea的最后一行或textarea的第一行与我的光标与JavaScript。 我想抓住第一个换行符和最后一个换行符的位置,然后抓住光标的位置。 var firstNewline = $('#myTextarea').val().indexOf('\n'); var lastNewline = $('#myTextarea').val().lastIndexOf('\n'); var cursorPosition = ?????; if (cursorPosition < firstNewline) // I am on first line. else if (cursorPosition > lastNewline) // I am on last line. 是否有可能抓住textarea内的光标位置? 你有更好的build议,看看我是否在textarea的第一行或最后一行? jQuery解决scheme首选,除非JavaScript是简单或更简单。 任何帮助深表感谢。

JavaScript中的'x <<〜y'代表什么?

JavaScript中的'x <<〜y'代表什么? 我明白,按位SHIFT操作这样做: x << y AS x * 2 y 而波浪号~操作符可以: ~x AS -(x+1) 所以,我假设如下: 5 <<〜3 AS 5 * 2 -4或5 * Math.pow(2,-4) 它应该导致0.3125 。 但是,当我运行5 << ~3 1342177280 ,结果是1342177280 。 什么是一步一步的解释? 如何以及为什么这个操作组合会导致1342177280而不是0.3125 ? (这个问题类似于Stack Overflow问题关于按位SHIFT 运算符,什么是按位运算符?

如何在JavaScript中join两个json对象,而不使用JQUERY

我有两个json对象obj1和obj2,我想合并它们并克里特一个单一的json对象。 得到的json应该包含obj2中的所有值和obj2中不存在的obj1中的值。 Question: var obj1 = { "name":"manu", "age":23, "occupation":"SE" } var obj2 = { "name":"manu", "age":23, "country":"india" } Expected: var result = { "name":"manu", "age":23, "occupation":"SE", "country":"india" }

获取在Java中的CPU核心数量?

有没有一种方法来确定在JavaScript中可用的CPU核心数量,以便您可以调整networking工作者的数量取决于?

parsingnode.js中的查询string

在这个“Hello World”示例中: // Load the http module to create an http server. var http = require('http'); // Configure our HTTP server to respond with Hello World to all requests. var server = http.createServer(function (request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); response.end("Hello World\n"); }); // Listen on port 8000, IP defaults to 127.0.0.1 server.listen(8000); // Put a […]