古老的线程,但有新的方法来运行等效的.isset()
ESNext(2019年12月4日阶段)
两种新的语法使我们能够大大简化功能的使用:isset()
请阅读文档并注意浏览器兼容性。
答
有关说明,请参见下文。注意:我使用标准JS语法
用法示例
// IMPORTANT pass a function to our isset() that returns the value we're
// trying to test(ES6 arrow function)
isset(() => some) // false
// Defining objects
let some = { nested: { value: 'hello' } }
// More tests that never throw an error
isset(() => some) // true
isset(() => some.nested) // true
isset(() => some.nested.value) // true
isset(() => some.nested.deeper.value) // false
// Less compact but still viable except when trying to use `this` context
isset(function () { return some.nested.deeper.value }) // false
应答功能
/**
* Checks to see if a value is set.
*
* @param {Function} accessor Function that returns our value
* @returns {Boolean} Value is not undefined or null
*/
function isset (accessor) {
try {
// Note we're seeing if the returned value of our function is not
// undefined or null
return accessor() !== undefined && accessor() !== null
} catch (e) {
// And we're able to catch the Error it would normally throw for
// referencing a property of undefined
return false
}
}
NPM 软件包
此应答功能可作为 NPM 上的软件包使用。该包包含一些改进,例如类型检查和支持多个参数。isset-php
npm install --save isset-php
自述文件中提供了完整的文档。
const isset = require('isset-php')
let val = ''
// This will evaluate to true so the text will be printed.
if (isset(() => val)) {
console.log('This val is set so I will print.')
}
解释
菲律宾比索
请注意,在 PHP 中,您可以引用任何深度的任何变量 - 即使尝试访问非数组作为数组,也会返回简单或:true
false
// Referencing an undeclared variable
isset($some); // false
$some = 'hello';
// Declared but has no depth(not an array)
isset($some); // true
isset($some['nested']); // false
$some = ['nested' => 'hello'];
// Declared as an array but not with the depth we're testing for
isset($some['nested']); // true
isset($some['nested']['deeper']); // false
JavaScript
在JavaScript中,我们没有这种自由;如果我们做同样的事情,我们总是会得到一个错误,因为引擎正在立即尝试访问的值,然后我们才能将其包装在我们的函数中,所以...deeper
isset()
// Common pitfall answer(ES6 arrow function)
const isset = (ref) => typeof ref !== 'undefined'
// Same as above
function isset (ref) { return typeof ref !== 'undefined' }
// Referencing an undeclared variable will throw an error, so no luck here
isset(some) // Error: some is not defined
// Defining a simple object with no properties - so we aren't defining
// the property `nested`
let some = {}
// Simple checking if we have a declared variable
isset(some) // true
// Now trying to see if we have a top level property, still valid
isset(some.nested) // false
// But here is where things fall apart: trying to access a deep property
// of a complex object; it will throw an error
isset(some.nested.deeper) // Error: Cannot read property 'deeper' of undefined
// ^^^^^^ undefined
更多失败的替代方案:
// Any way we attempt to access the `deeper` property of `nested` will
// throw an error
some.nested.deeper.hasOwnProperty('value') // Error
// ^^^^^^ undefined
// Similar to the above but safe from objects overriding `hasOwnProperty`
Object.prototype.hasOwnProperty.call(some.nested.deeper, 'value') // Error
// ^^^^^^ undefined
// Same goes for typeof
typeof some.nested.deeper !== 'undefined' // Error
// ^^^^^^ undefined
以及一些可以快速冗余的工作替代方案:
// Wrap everything in try...catch
try {
if (isset(some.nested.deeper)) {
// ...
}
} catch (e) {}
try {
if (some.nested.deeper !== undefined && some.nested.deeper !== null) {
// ...
}
} catch (e) {}
// Or by chaining all of the isset which can get long
isset(some) && isset(some.nested) && isset(some.nested.deeper) // false
// ^^^^^^ returns false so the next isset() is never run
结论
所有其他答案 - 尽管大多数都是可行的...
- 假设您只是检查变量是否未定义,这对于某些用例来说很好,但仍可能引发错误
- 假设您只是尝试访问顶级属性,这对于某些用例来说也很好。
- 强制您使用相对于PHP的
不太理想的方法,例如isset()
isset(some, 'nested.deeper.value')
- 使用哪个有效,但我个人避免
eval()
我想我涵盖了很多。我在回答中提出的一些观点我没有触及,因为它们虽然相关,但不是问题的一部分(例如短路)。但是,如果需要,我可以根据需要使用指向一些更技术性方面的链接来更新我的答案。
我花了很多时间在这上面,所以希望它能帮助人们。
感谢您的阅读!