[英]javascript - check if object is a function's arguments
In javascript you can get the arguments of a function as an array-like object via the keyword arguments
. 我想知道是否有辦法檢查 object 是否源自arguments
關鍵字。
(What i basically wanna do is this: if(myvar.constructor.name === "Arguments") {(...)}
, but the arguments object uses the default Object class, which is why this isn't possible. )
(讓我相信這樣做是可能的,瀏覽器在登錄到控制台時將 arguments object 標記為“參數”)
您可以將其轉換為字符串並將其與[object Arguments]
進行比較:
const isArguments = param => param.toString() === '[object Arguments]'; function foo() { console.log(isArguments(arguments)); console.log(isArguments({})); } foo();
檢測它們的唯一方法是使用Obect.prototype.toString
"class" check :
if (Object.prototype.toString.call(myVar) == "[object Arguments]")
從 ES6 開始,這可以被[Symbol.toStringTag]
屬性愚弄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.