var obj = {
name: 'hello',
getName: function(){
return () => {return this.name; }
}
}
var name = 'world';
var nameFunc = obj.getName();
console.log(nameFunc())
Run Code Online (Sandbox Code Playgroud)
结果是"你好",而不是"世界".我有点困惑.
请参阅以下代码段:
片段#1:
let fs = require("fs");
fs.readFile(process.argv[2], "utf8", (error, data) => console.log(arguments));Run Code Online (Sandbox Code Playgroud)
小片#2:
let fs = require("fs");
fs.readFile(process.argv[2], "utf8", (error, data) => console.log(error, data));Run Code Online (Sandbox Code Playgroud)
预期日志:
值(error, data),例如:
null 'console.log("HELLO WORLD");\r\n'
当您尝试这两个片段时,您会发现Snippet#1会执行并记录一些意外的值,console.log(arguments)但会console.log(error, data)记录正确的值; 价值观(error, data).
为什么以及为Snippet#1记录的值是多少?
我不明白 React 应用程序中 vanilla javascript 和 ES6 之间的语法差异。我的第一个不起作用的代码是
class App extends Component{
constructor(props){
super(props);
this.state = {videos:[]};
YTSearch({key: API_KEY,term :'surfboards'},function(videos){
this.setState({videos : videos});
});
}
Run Code Online (Sandbox Code Playgroud)
这会在控制台中出现“无法读取未定义属性”的“setState”错误
但将语法更改为
YTSearch({key: API_KEY,term :'surfboards'},(videos)=>{
this.setState({videos : videos});
});
Run Code Online (Sandbox Code Playgroud)
解决问题。两者不是一回事(我可能错了)。使用
function(videos){}
Run Code Online (Sandbox Code Playgroud)
和
(videos) => {}
Run Code Online (Sandbox Code Playgroud)
我对 javascript 不满意,因此感谢您的任何帮助。
I would like to execute functions one at a time and call another function when a function is finished. I was able to do it using callbacks but not using a promise chain. Here is what I tried (based on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#chained_promises) but it executes the first 3 functions at the same time instead of waiting 1 second in each function:
function displayAll() {
var myPromise = (new Promise(display1))
.then((new Promise(display2))
.then((new Promise(display3))
.then(display4)));
}
function display1(resolve) {
setTimeout(function () …Run Code Online (Sandbox Code Playgroud) 我在一篇教程中看到了这段代码片段:
\nconst myFunction = () => {\n return function (caller) {\n caller(firstFuctnion());\n caller(secondFunction());\n };\n};\nRun Code Online (Sandbox Code Playgroud)\n一旦我这样称呼它,你能告诉我它是如何工作的吗:
\nmyFunction()\nRun Code Online (Sandbox Code Playgroud)\ncaller当参数实际上未定义时,为什么我没有 \xe2\x80\x99t 收到错误?
另一方面,如果我省略它并编写如下代码:
\nconst myFunction = () => {\n return function () {\n firstFuctnion();\n secondFunction();\n };\n };\nRun Code Online (Sandbox Code Playgroud)\n这两个函数firstFuction()和secondFunction()\xe2\x80\x99t 被执行。那么,caller\xe2\x80\x8a\xe2\x80\x94\xe2\x80\x8a 到底是如何调用\xe2\x80\x80\x99\xe2\x80\x8a\xe2\x80\x94\xe2\x80\ x8a 工作吗?
对于那些可能想查看完整功能代码的人:
\nconst myFunction = () => {\n return function (caller) {\n caller(firstFuctnion());\n caller(secondFunction());\n };\n};\nRun Code Online (Sandbox Code Playgroud)\r\n我不明白这部分。那dispatch实际上是做什么的。它没有作为参数传递,也没有在任何地方定义它是什么。是功能吗?我可以whatever在那里写。 …
我试图在ng2中找到路由,我遇到了一个奇怪的问题.当用户到达'/ home'时,我尝试设置超时以将我导航回'/'.
这有效:
export class HomeComponent implements OnInit {
constructor(private router: Router) { }
ngOnInit() {
setTimeout(() => {this.router.navigate(['/']);}, 2000);
}
}
Run Code Online (Sandbox Code Playgroud)
但这不是:
export class HomeComponent implements OnInit {
constructor(private router: Router) { }
ngOnInit() {
setTimeout(function () {this.router.navigate(['/']);}, 2000);
}
}
Run Code Online (Sandbox Code Playgroud)
它失败了 - EXCEPTION: Cannot read property 'navigate' of undefined
为了使它工作,我必须将其更改为:
export class HomeComponent implements OnInit {
constructor(private router: Router) { }
ngOnInit() {
var _router = this.router;
setTimeout(function (_router) {_router.navigate(['/']);}, 2000, _router);
}
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句,这是TypeScript编译的方式() => {} …
let objNewWay = {
width:400,
height:300,
area: () => this.width*this.height
};
console.log(objNewWay.area()); // NaN
let objOldWay = {
width:400,
height:300,
area: function() {
return this.width*this.height;
}
};
console.log(objOldWay.area()); // 120000
Run Code Online (Sandbox Code Playgroud)
我不明白为什么 Javascript 对象中的箭头函数似乎不起作用。如果您查看上面的代码,第一个 console.log 将打印 NaN,第二个 console.log 将按预期打印数字。
我有一个Date变量,我每秒更新它以使其生效.
现在这是我的变量.
var stationdate = new Date(data.localTime);
Run Code Online (Sandbox Code Playgroud)
我的Javascript代码每秒更新一次.
window.setInterval(function () {
stationdate = new Date(stationdate.setSeconds(stationdate.getSeconds() + 1));
}, 1000);
Run Code Online (Sandbox Code Playgroud)
和我的类型脚本代码将其返回到Angular UI.
window.setInterval(() => this.time = stationdate, 1000);
Run Code Online (Sandbox Code Playgroud)
我的问题.
如果两个函数都是分离的,它可以完美地工作.
但是如果我将它们组合起来就会停止工作
见下文.
window.setInterval(function () {
stationdate = new Date(stationdate.setSeconds(stationdate.getSeconds() + 1));
this.time = stationdate;
}, 1000);
Run Code Online (Sandbox Code Playgroud)
AM I I I WITH WITH FAT FAT FAT FAT FAT FAT FAT FAT FAT FAT?
什么应该是正确的功能?
我当时在React.js应用程序上工作,最初使用的是箭头功能,但出于好奇,我决定尝试正常功能,但正常功能无效。我认为他们都应该输出相同的内容,这是怎么回事?
handleChange = event => this.setState({init: event.target.value})
handleChange(event){
this.setState({init: event.target.value})
}
Run Code Online (Sandbox Code Playgroud) 我通常不用javascript编写,并且自己写了一些问题.我写这样,当用户点击了一个捐赠金额按钮(.amountNum输入[类型="无线电"])捐赠页面上,它打印捐赠总额多少会下面的代码位与一些在线捐赠软件进行交互如果他们支付处理费.除了Internet Explorer之外,每个浏览器都有效.原来箭头功能在IE中不起作用,我不知道如何在没有它的情况下重写该功能.有什么指针吗?
var inputs = document.querySelectorAll(".amountNum input[type='radio']");
inputs.forEach(el =>
el.addEventListener('click', function() {
var value = parseInt(this.value, 10);
var URL = `https://example.com/display-fee?contribution=${value}&max=2500.00`;
jQuery.get(URL, function(data) {
var fee = data.fee;
var total = fee + value;
total = total.toFixed(2);
$("#full-gift-label")[0].innerText = ("I'd like to help cover the transaction fees on my donation. My grand total will be £" + total);
})
}));
Run Code Online (Sandbox Code Playgroud) javascript ×10
ecmascript-6 ×3
angular ×2
reactjs ×2
typescript ×2
global ×1
jquery ×1
node.js ×1
object ×1
promise ×1
redux ×1
scope ×1
settimeout ×1
this ×1