JavaScript 中 void 运算符的作用是什么?

发布于 2024-07-15 08:07:13 字数 186 浏览 9 评论 0原文

我见过有些人在代码中使用 void 运算符。 我还在 href 属性中看到了这一点: javascript:void(0) 似乎并不比 javascript:;

那么,什么使用 void 运算符的理由是什么?

I've seen some people using void operator in their code. I have also seen this in href attributes: javascript:void(0) which doesn't seem any better than javascript:;

So, what is the justification of using the void operator?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

御守 2024-07-22 08:07:13

其使用说明在链接中:

这就是书签的原因
通常将代码包装在 void() 或
一个匿名函数,它不
返回任何内容以停止浏览器
尝试显示结果
执行小书签。 为了
示例:

javascript:void(window.open("dom_spy.html")) 
  

如果直接使用返回的代码
某事(一个新的窗口实例
在这种情况下),浏览器将最终
显示:

javascript:window.open("dom_spy.html"); 
  

在 Firefox 中,上面将显示:

[对象窗口] 
  

Explanation of its use in links:

This is the reason that bookmarklets
often wrap the code inside void() or
an anonymous function that doesn't
return anything to stop the browser
from trying to display the result of
executing the bookmarklet. For
example:

javascript:void(window.open("dom_spy.html"))

If you directly use code that returns
something (a new window instance in
this case), the browser will end up
displaying that:

javascript:window.open("dom_spy.html");

In Firefox the above will display:

[object Window]
篱下浅笙歌 2024-07-22 08:07:13

直到 JS1.3 之前,undefined 值在 JavaScript 中无法直接访问。

因此包含了一个运算符 void 以允许访问此值。

有时,它很有用,特别是在使用 Web API(例如事件处理程序)时,可确保表达式的结果始终未定义

undefined 属性被添加到 JS1.3 中的全局对象时,void 的实用性变得不明显。

因此你的问题。

The undefined value was not directly accessible in JavaScript until JS1.3.

An operator void <expression> was therefore included to permit access to this value.

It is sometimes useful, particularly when working with the Web API (e.g. event handlers), to ensure that the result of an expression is consistently undefined.

When the undefined property was added to the global object in JS1.3 the utility of void became non-obvious.

Hence your question.

如若梦似彩虹 2024-07-22 08:07:13

JavaScript 中,void 运算符用于显式返回未定义的值。 它是一个一元运算符,这意味着只能使用一个操作数。 您可以如下所示使用它 - 独立使用或与括号一起使用。

void expression;
void(expression);

让我们看一些例子:

void 0; //returns undefined
void(1); //returns undefined

void 'hello'; //undefined
void {}; //undefined
void []; //undefined

void myFunction(); 
void(myFunction());

如果你问为什么需要一个特殊的关键字来返回 undefined 而不是仅仅返回 undefined:原因是在 ES5 之前你实际上可以命名一个全局变量 undefined< /code>,像这样:var undefined = "hello"var undefined = 23,大多数浏览器都会接受它; 标识符未定义并没有承诺实际上是未定义的。 因此,为了返回实际未定义值,使用了void运算符。 但它并不是一个非常流行的运算符,并且很少使用。

让我们看一个带有 void 的函数示例:

//just a normal function
function test() {
    console.log('hello');
    return 2;
}

//lets call it
console.log(test()); //output is hello followed by 2

//now lets try with void
console.log(void test()); //output is hello followed by undefined

void 丢弃函数的返回值并显式返回 undefined。

您可以从我的教程文章中阅读更多内容: https://josephkhan.me/the-javascript- void-operator/

1 在 ECMAScript 5 及更高版本中,全局变量 undefined 保证是未定义的 (ECMA-262 第五版,§ 15.1.1.3),尽管它是仍然可以将内部作用域内的变量命名为 undefined

The JavaScript, the void operator is used to explicitly return the undefined value. It's a unary operator, meaning only one operand can be used with it. You can use it like shown below — standalone or with a parenthesis.

void expression;
void(expression);

Lets see some examples:

void 0; //returns undefined
void(1); //returns undefined

void 'hello'; //undefined
void {}; //undefined
void []; //undefined

void myFunction(); 
void(myFunction());

If you ask why do you need a special keyword just to return undefined instead of just returning undefined: the reason is that before ES5 you could actually name a global variable undefined, like so: var undefined = "hello" or var undefined = 23, and most browsers would accept it; the identifier undefined was not promised to actually be undefined¹. So, to return the actual undefined value, the void operator is/was used. It's not a very popular operator though and is seldom used.

Let's see an example of function with void:

//just a normal function
function test() {
    console.log('hello');
    return 2;
}

//lets call it
console.log(test()); //output is hello followed by 2

//now lets try with void
console.log(void test()); //output is hello followed by undefined

void discards the return value from the function and explicitly returns undefined.

You can read more from my tutorial post: https://josephkhan.me/the-javascript-void-operator/

¹ In ECMAScript 5 and later, the global variable undefined is guaranteed to be undefined (ECMA-262 5th Ed., § 15.1.1.3), though it is still possible to have a variable inside an inner scope be named undefined.

白云不回头 2024-07-22 08:07:13

请考虑以下事项:

<a href="javascript:void(fish=document.getElementById('foo').value);void(document.getElementById('bar').value=fish);">With Void</a>

<a href="javascript:fish=document.getElementById('foo').value;document.getElementById('bar').value=fish;">Without Void</a>

<input type="text" id="foo" value="one fish" />
<input type="text" id="bar" value="no fish" />

第一个链接将交换文本字段的值。 第二个链接将打开一个新页面,其中包含文本“一条鱼”。 如果您使用 javascript: link,一旦表达式返回 nullundefined 以外的内容,浏览器就会将其解释为链接的内容应该做。 通过将所有表达式/语句包装在 void() 函数中,您可以确保整个代码片段都能运行。 如今,这主要在 Bookmarklet 中使用,因为使用 onclick 属性或在单独的 Javascript 块/文件中设置事件处理程序是“规范”。

至于 javascript:javascript:void(),第一个语句是不明确的。 你说,“嘿,我想运行一些 javascript”,但你没有提供任何代码。 浏览器在这里应该做什么并不一定很清楚。 对于第二个语句,您说“嘿,运行一些 javascript”,并且您的代码最终返回未定义,浏览器知道这意味着“不执行任何操作”。

既然我在这里,我还要指出,使用 javascript:javascript:void(); 已经不再受到大多数关心标记的人的青睐。 更好的做法是让 onclick 处理程序返回 false,并让链接指向对关闭 javascript 或使用 JavaScript 拦截器(例如 NoScript)的人有意义的页面/资源。

Consider the following:

<a href="javascript:void(fish=document.getElementById('foo').value);void(document.getElementById('bar').value=fish);">With Void</a>

<a href="javascript:fish=document.getElementById('foo').value;document.getElementById('bar').value=fish;">Without Void</a>

<input type="text" id="foo" value="one fish" />
<input type="text" id="bar" value="no fish" />

The first link will swap the values of the text fields. The second link will open a new page with the text "one fish". If you use a javascript: link, the minute an expression returns something other than null or undefined, the browser will interpret that as what the link should do. By wrapping all expressions/statments in a void() function, you ensure your entire snippet of code will run. These days, this is primarily of use in Bookmarklets, as using an onclick attribute, or setting up event handlers in separate Javascript blocks/files is the "norm".

As for javascript: vs. javascript:void(), the first statement is ambiguous. You're saying, "hey, I want to run some javascript", but then you don't provide any code. It's not necessarily clear what the browser should do here. With the second statement you're saying "hey, run some javascript", and your code eventually returns undefined, which the browser knows means "do nothing".

Since I'm here, I'll also point out that using either javascript: or javascript:void(); has fallen out of favor with most people who care about markup. The better thing to do is have your onclick handler return false, and have the link pointed towards a page/resource that makes sense for people who have javascript turned off, or are using a javascript blocker such as NoScript.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文