带有退货声明的单线

can*_*era 1 javascript

有没有办法将下面的JavaScript代码编写为单行代码?

this.isContactPage = (window.location.pathname === '/contact');
if (!this.isContactPage) return false;
Run Code Online (Sandbox Code Playgroud)

如果this.isContactPage为真,则该方法继续.

this.isContactPage脚本中的其他位置需要该属性.

Ami*_*ich 5

return !(this.isContactPage = (window.location.pathname === '/contact'));
Run Code Online (Sandbox Code Playgroud)

另一个例子:

console.log(window.prop); // undefined
console.log(!(window.prop = true) || undefined); // undefined
console.log(!(window.prop = true)); // false
console.log(window.prop); // true
Run Code Online (Sandbox Code Playgroud)