我试图在Javascript中包围我的头.
以下是教程中的示例:
function greeter(name, age) {
var message = name + ", who is " + age + " years old, says hi!";
return function greet() {
console.log(message);
};
}
// Generate the closure
var bobGreeter = greeter("Bob", 47);
// Use the closure
bobGreeter();
Run Code Online (Sandbox Code Playgroud)
作者说这是使用闭包来制作私有变量的有效方法,但我不明白这一点.
有人可以启发像这样编码的好处吗?
当我尝试这样做时:
function a()
{
function b() { }
}
a();
a();
Run Code Online (Sandbox Code Playgroud)
我得到Cannot redeclare b....
当我试图这样做时:
function a()
{
if(!isset(b))
function b() { }
}
a();
a();
Run Code Online (Sandbox Code Playgroud)
我得到unexpected ), expected ....
如何将函数声明为本地函数并在a返回时将其遗忘?我需要将函数传递给它array_filter.
我看到下面的JavaScript函数完全相同,那么除了语法之外,它们之间有什么区别.功能是:
var functionName=function(){
//some code here
};
function functionName(){
//some code here
}
Run Code Online (Sandbox Code Playgroud)
我用以下的方式打电话给他们:
functionName();
Run Code Online (Sandbox Code Playgroud)
请不要告诉我语法有所不同,除此之外还有什么不同之处
1)speed of execution
2)Memory utilization etc.
Run Code Online (Sandbox Code Playgroud)
提前致谢!
我一直以为浏览器会逐字逐句地从上到下执行JavaScript代码(你有点期望这种行为来自脚本语言).但显然情况并非如此:
//工作完美
<script>
test();
function test() { alert('test'); }
</script>
Run Code Online (Sandbox Code Playgroud)
但如果我将函数声明为变量,它将失败并显示'未捕获的ReferenceError:未定义测试':
<script>
test();
var test = function() { alert('test'); }
</script>
Run Code Online (Sandbox Code Playgroud)
所以javascript引擎有时不会从上到下执行代码.它可以以某种方式预加载函数,即使它们最终被声明.它究竟是如何工作的?为什么?
正如所看到这里,有函数声明和函数表达式之间的一些差异。
函数表达式与函数声明相比有一个缺点,如果在声明之前对其进行调用,它将产生错误。
我只想知道使用函数表达式的好处,因为我似乎只看到上面刚刚提到的缺点。我可以举个例子...
函数表达式:
alert(foo()); // ERROR! foo wasn't loaded yet
var foo = function() { return 5; }
Run Code Online (Sandbox Code Playgroud)
函数声明:
alert(foo()); // Alerts 5. Declarations are loaded before any code can run.
function foo() { return 5; }
Run Code Online (Sandbox Code Playgroud) 我碰巧遇到了以下奇怪的情况:
其中一个网络调用返回了这样的响应:
window.function1 = function() {
console.log('function 1');
} window.project = 'test';
Run Code Online (Sandbox Code Playgroud)
但是,当对以下脚本进行评估时,它将返回错误
意外的标识符
semi-colon在function1定义之后添加a时,此问题会得到修复因此正确的修复方法是:
window.function1 = function() {
console.log('function 1');
}; window.project = 'test';
Run Code Online (Sandbox Code Playgroud)
我很想知道这背后的原因.
在Firefox 43上使用以下代码打开名为index.html的文件会出现以下错误:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script>
"use strict";
class RangeIterator {}
</script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我在控制台中看到以下错误:
SyntaxError: class is a reserved identifier
Run Code Online (Sandbox Code Playgroud)
知道我为什么会收到这个错误吗?
我正在尝试使用window.postMessage API将子文档(iframe)的简单消息发送回其直接父文档。
在父文档中,我具有以下内容:
window.addEventListener("message", receiveMessage, true);
var receiveMessage = function(event) {
console.log("Recieved event " + JSON.stringify(event));
}
Run Code Online (Sandbox Code Playgroud)
然后,在iframe中,我有以下内容:
window.parent.postMessage('message', '*');
Run Code Online (Sandbox Code Playgroud)
根据我阅读的所有内容,这应该可以正常工作,并且我的日志消息应该写入控制台。除非它不起作用。
我知道将*用作targetOrigin并不总是安全的,但是在这一点上,我只想整理一下链接。
有什么想法或明显的我想念的东西吗?
I am working on a javascript code where functions are defined in three different ways.
funtion f1(){}
Run Code Online (Sandbox Code Playgroud)
and second
var vaiable = f1(){}
Run Code Online (Sandbox Code Playgroud)
and third
window.f1 = function(){}
Run Code Online (Sandbox Code Playgroud)
I have read about the first two here but don't know about the last one.
Will there be a problem if I change the third one to the second one?
What are the pros and cons of third type?
Why it is used particularly?
只是想问一个问题:在JavaScript中将函数命名为变量有什么理由吗?
(我的意思是使用Dep两次。只是在Vue.js中爬行并找到了这个)
var Dep = function Dep () {
this.id = uid++;
this.subs = [];
};
Run Code Online (Sandbox Code Playgroud) javascript ×9
function ×4
closures ×1
ecmascript-6 ×1
html ×1
html5 ×1
iframe ×1
jquery ×1
minify ×1
php ×1
postmessage ×1
vue.js ×1