最新記事

Node.js - SyntaxError: 予期しないトークンのインポート 質問する
javascript
node.js
npm
ecmascript-6

Node.js - SyntaxError: 予期しないトークンのインポート 質問する

何が問題なのか分かりません。Node v5.6.0 NPM v3.10.6 コード: function (exports, require, module, __filename, __dirname) { import express from 'express' }; エラー: SyntaxError: Unexpected token import at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:387:25) at Object.Module._extensions..js (module.js:422:10) at Module.load (module.js:357:32) at Function.Module._load (module.js:314:12) at Function.Module.runMain (module.js:447:10) at startup (node.js:140:18) at node.js:1001:3

Admin

jQuery: ajax 呼び出しが成功した後にデータを返す [重複] 質問する
javascript
jquery
ajax

jQuery: ajax 呼び出しが成功した後にデータを返す [重複] 質問する

この質問に対する回答は既にここにあります : 非同期呼び出しから応答を返すにはどうすればいいですか? (42件の回答) 11年前に 閉店しました 。 これに似たものがあり、これは値、つまり文字列を返すスクリプトへの単純な呼び出しです。 function testAjax() { $.ajax({ url: "getvalue.php", success: function(data) { return data; } }); } しかし、もし私がこのようなものを呼び出すと var output = testAjax(svar); // output will be undefined... では、値を返すにはどうすればよいのでしょうか? 以下のコードも機能しないようです... function testAjax() { $.ajax({ url: "getvalue.php", success: function(data) { } }); return data; }

Admin

JavaScript closures vs. anonymous functions Ask Question
javascript
scope
closures

JavaScript closures vs. anonymous functions Ask Question

A friend of mine and I are currently discussing what is a closure in JS and what isn't. We just want to make sure we really understand it correctly. Let's take this example. We have a counting loop and want to print the counter variable on the console delayed. Therefore we use setTimeout and closures to capture the value of the counter variable to make sure that it will not print N times the value N. The wrong solution without closures or anything near to closures would be: for(var i = 0; i < 10; i++) { setTimeout(function() { console.log(i); }, 1000); } which will of course print 10 times the value of i after the loop, namely 10. So his attempt was: for(var i = 0; i < 10; i++) { (function(){ var i2 = i; setTimeout(function(){ console.log(i2); }, 1000) })(); } printing 0 to 9 as expected. I told him that he isn't using a closure to capture i, but he insists that he is. I proved that he doesn't use closures by putting the for loop body within another setTimeout (passing his anonymous function to setTimeout), printing 10 times 10 again. The same applies if I store his function in a var and execute it after the loop, also printing 10 times 10. So my argument is that he doesn't really capture the value of i, making his version not a closure. My attempt was: for(var i = 0; i < 10; i++) { setTimeout((function(i2){ return function() { console.log(i2); } })(i), 1000); } So I capture i (named i2 within the closure), but now I return another function and pass this around. In my case, the function passed to setTimeout really captures i. Now who is using closures and who isn't? Note that both solutions print 0 to 9 on the console delayed, so they solve the original problem, but we want to understand which of those two solutions uses closures to accomplish this.

Admin

JavaScript で矢印キーの押下を検出する 質問する
javascript
keypress
keyboard-events

JavaScript で矢印キーの押下を検出する 質問する

矢印キーの 1 つが押されたことを検出するにはどうすればよいですか? 私はこれを調べるためにこれを使用しました: function checkKey(e) { var event = window.event ? window.event : e; console.log(event.keyCode) } 他のすべてのキーでは機能しましたが、矢印キーでは機能しませんでした (ブラウザがデフォルトでこれらのキーでスクロールすることになっているためと思われます)。

Admin

JavaScript を使用して配列からオブジェクトを削除する 質問する
javascript
arrays

JavaScript を使用して配列からオブジェクトを削除する 質問する

配列からオブジェクトを削除するにはどうすればよいですか? 配列Kristianからname を含むオブジェクトを削除したいと思いますsomeArray。例: someArray = [{name:"Kristian", lines:"2,5,10"}, {name:"John", lines:"1,19,26,96"}]; 達成したいこと: someArray = [{name:"John", lines:"1,19,26,96"}];

Admin

JavaScriptで文字列を何回か繰り返す 質問する
javascript
string
character
repeat

JavaScriptで文字列を何回か繰り返す 質問する

Perl では、次の構文を使用して文字を複数回繰り返すことができます。 $a = "a" x 10; // results in "aaaaaaaaaa" Javascript でこれを実現する簡単な方法はありますか? もちろん関数を使うこともできますが、組み込みのアプローチや他の巧妙なテクニックがあるかどうか知りたいです。

Admin

ネストされた JavaScript オブジェクトキーの存在をテストする 質問する
javascript
object
properties
nested

ネストされた JavaScript オブジェクトキーの存在をテストする 質問する

オブジェクトへの参照がある場合: var test = {}; 次のようなネストされたオブジェクトが含まれる可能性があります (ただし、すぐに含まれるわけではありません)。 {level1: {level2: {level3: "level3"}}}; 深くネストされたオブジェクト内のプロパティの存在を確認する最良の方法は何ですか? alert(test.level1);得られますundefinedが、alert(test.level1.level2.level3);失敗します。 私は現在、次のようなことを行っています: if(test.level1 && test.level1.level2 && test.level1.level2.level3) { alert(test.level1.level2.level3); } しかし、もっと良い方法があるのではないかと考えていました。

Admin

大文字と小文字を区別しない検索 質問する
javascript
search
string-comparison
case-insensitive

大文字と小文字を区別しない検索 質問する

JavaScript で 2 つの文字列を使用して大文字と小文字を区別しない検索を実行しようとしています。 通常は次のようになります。 var string="Stackoverflow is the BEST"; var result= string.search(/best/i); alert(result); この/iフラグは大文字と小文字を区別しないことを意味します。 しかし、2 番目の文字列を検索する必要があります。フラグなしでも完璧に動作します。 var string="Stackoverflow is the BEST"; var searchstring="best"; var result= string.search(searchstring); alert(result); 上記の例にフラグを追加すると、/i変数「searchstring」内の内容ではなく、searchstring が検索されます (次の例は機能しません)。 var string="Stackoverflow is the BEST"; var searchstring="best"; var result= string.search(/searchstring/i); alert(result); どうすればこれを実現できるでしょうか?

Admin

React onClick関数はレンダリング時に実行されます 質問する
javascript
reactjs
button
onclick
dom-events

React onClick関数はレンダリング時に実行されます 質問する

子コンポーネントに 2 つの値を渡します。 表示するオブジェクトのリスト 削除機能。 私はオブジェクトのリストを表示するために .map() 関数を使用します (React チュートリアル ページの例のように)。しかし、そのコンポーネントのボタンはonClickレンダリング時に関数を起動します (レンダリング時に起動するべきではありません)。私のコードは次のようになります。 module.exports = React.createClass({ render: function(){ var taskNodes = this.props.todoTasks.map(function(todo){ return (

{todo.task}
); }, this); return (
{taskNodes}
); } }); 私の質問は、onClickレンダリング時に関数が実行される理由と、実行しないようにする方法です。

Admin