Questions tagged «javascript»

有关在ECMAScript(JavaScript / JS)及其各种方言/实现(不包括ActionScript)中进行编程的问题。此标记很少单独使用,但最常与标记[node.js],[jquery],[json]和[html]关联。

17
如何在javascript中舍入浮点数?
我需要一轮例如6.688689到6.7,但它总是显示我7。 我的方法: Math.round(6.688689); //or Math.round(6.688689, 1); //or Math.round(6.688689, 2); 但是结果总是一样的7……我在做什么错?





13
检测HTTP或HTTPS,然后在JavaScript中强制HTTPS
有什么方法可以检测HTTP或HTTPS,然后通过JavaScript强制使用HTTPS? 我有一些用于检测HTTP或HTTPS的代码,但是我不能强迫它使用https:。 我正在使用window.location.protocol属性设置站点的任何内容,https:然后刷新页面以希望将新的httpsed URL重新加载到浏览器中。 if (window.location.protocol != "https:") { window.location.protocol = "https:"; window.location.reload(); }

15
从fs.readFile获取数据
var content; fs.readFile('./Index.html', function read(err, data) { if (err) { throw err; } content = data; }); console.log(content); 日志undefined,为什么?


7
在动作创建者中访问Redux状态?
说我有以下几点: export const SOME_ACTION = 'SOME_ACTION'; export function someAction() { return { type: SOME_ACTION, } } 在该动作创建者中,我想访问全局商店状态(所有reducer)。这样做更好吗? import store from '../store'; export const SOME_ACTION = 'SOME_ACTION'; export function someAction() { return { type: SOME_ACTION, items: store.getState().otherReducer.items, } } 或这个: export const SOME_ACTION = 'SOME_ACTION'; export function someAction() { return (dispatch, …
296 javascript  redux 

19
如何获取JavaScript对象的大小?
我想知道一个JavaScript对象占用的大小。 具有以下功能: function Marks(){ this.maxMarks = 100; } function Student(){ this.firstName = "firstName"; this.lastName = "lastName"; this.marks = new Marks(); } 现在我实例化student: var stud = new Student(); 这样我就可以做 stud.firstName = "new Firstname"; alert(stud.firstName); stud.marks.maxMarks = 200; 等等 现在,stud对象将在内存中占据一些大小。它具有一些数据和更多对象。 我如何找出stud对象占用多少内存?像sizeof()JavaScript中的?如果我能在单个函数调用中找到它,那将真的很棒sizeof(stud)。 我已经在互联网上搜索了几个月了-找不到它(在几个论坛中提问-没有回复)。

10
如何为Internet Explorer浏览器修复JavaScript中的数组indexOf()
如果您曾经使用过JavaScript,那么您就会知道Internet Explorer不会为Array.prototype.indexOf()[包括Internet Explorer 8]实现ECMAScript函数。这不是一个大问题,因为您可以使用以下代码扩展页面上的功能。 Array.prototype.indexOf = function(obj, start) { for (var i = (start || 0), j = this.length; i < j; i++) { if (this[i] === obj) { return i; } } return -1; } 我应该何时实施? 我是否应该使用以下检查将其包装在我的所有页面上,该检查将检查原型函数是否存在,如果不存在,请继续并扩展Array原型? if (!Array.prototype.indexOf) { // Implement function here } 还是要检查浏览器,如果它是Internet Explorer,则只需实施它? //Pseudo-code if …

7
将道具传递给React.js中的父组件
props在React.js中,没有一种简单的方法可以通过事件将孩子的孩子传递给父母吗? var Child = React.createClass({ render: function() { <a onClick={this.props.onClick}>Click me</a> } }); var Parent = React.createClass({ onClick: function(event) { // event.component.props ?why is this not available? }, render: function() { <Child onClick={this.onClick} /> } }); 我知道您可以使用受控组件来传递输入值,但是最好传递整个工具包n'kaboodle。有时,子组件包含一些您不想查找的信息。 也许有一种方法可以将组件绑定到事件? 更新– 9/1/2015 使用后反应了一年多,和塞巴斯蒂安·洛伯的回答的刺激下,我已经得出结论传递子组件作为参数传递给函数的父母是不是事实上的方式做出反应,也不是永远的好主意。我已经换了答案。

17
获取JavaScript对象键列表
我有一个JavaScript对象,例如 var obj = { key1: 'value1', key2: 'value2', key3: 'value3', key4: 'value4' } 如何获得此对象中键的长度和列表?
295 javascript 

15
JavaScript-onClick获取被单击按钮的ID
如何找到被点击的按钮的ID? <button id="1" onClick="reply_click()"></button> <button id="2" onClick="reply_click()"></button> <button id="3" onClick="reply_click()"></button> function reply_click() { }
295 javascript  html 

23
如何从JavaScript中的字符串修剪文件扩展名?
例如,假设x = filename.jpg,我想得到filename,其中filename可以是任何文件名(让我们假设该文件名仅包含[a-zA-Z0-9-_]来简化。)。 我x.substring(0, x.indexOf('.jpg'))在DZone片段上看到了,但是效果会x.substring(0, x.length-4)更好吗?因为,length是属性,不进行字符检查,indexOf()而是函数,则进行字符检查。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.