草庐IT

InstanceOf

全部标签

javascript - 如何在 QML 中执行 "is_a"、 "typeof"或 instanceof ?

我想遍历QML组件列表并选择一种类型:for(vari=0;i如何做到这一点? 最佳答案 从Qt5.10开始,您终于可以使用instanceOf来检查变量是否属于某种QML类型,参见"QMLSupportforEnumandInstanceOfTypeChecks".importVPlayApps1.0importQtQuick2.0App{//twoQMLitems,usedfortypecheckingItem{id:testItem}Rectangle{id:testRect}//functiontocheckwheteran

javascript - 如何在 JavaScript 中将原始字符串(字符串文字)设为 "instanceof"

这个问题在这里已经有了答案:Whydoesinstanceofreturnfalseforsomeliterals?(10个答案)关闭9年前。在JavaScript中,我可以通过以下方式声明一个字符串;vara="HelloWorld";varb=newString("HelloWorld");但是a不是String的实例...console.log(ainstanceofString);//false;console.log(binstanceofString);//true;那么如何找到字符串文字的类型或“instanceof”?可以强制JavaScript为每个字符串文字创建一个

JavaScript instanceof 和 moment.js

我正在尝试理解JavaScript世界中的类型。我的页面正在使用moment.js.我有一个函数,有时会返回一个moment(),有时会返回一个string(它是遗留代码)。我的代码看起来像这样:varnow=getDate();if(nowinstanceofmoment){console.log('wehaveamoment.');}else{console.log('wehaveastring.');}functiongetDate(){varresult=null;//Sometimesresultwillbeamoment(),othertimes,resultwillbea

javascript - 使用 Array.isArray 和 instanceof Array 的区别

有两种方法可以判断一个数组是数组还是对象。使用typeofitem==="object";将为对象和数组返回true,因为数组对于javascript来说相对较新,并且数组是对象的原型(prototype)(可能措辞有误,请随时更正我)。所以我知道的两种确定数组是否为数组的方法是:解决方案一:Array.isArray(item);解决方案2:iteminstanceofArray;我的问题是:这两种解决方案有什么区别?这两个方案中哪一个是首选解决方案?哪个处理时间更快? 最佳答案 1.Whatisthedifferencebetw

PHPUnit、模拟接口(interface)和 instanceof

有时在我的代码中,我会检查特定对象是否实现了接口(interface):if($instanceinstanceofInterface){};但是,在PHPUnit中创建所述接口(interface)的模拟,我似乎无法通过该测试。//classnameisMock_Interface_431469d7,doesnotpassabovecheck$instance=$this->getMock('Interface');我知道拥有一个名为Interface的类与实现Interface的类不同,但我不知道如何处理这个问题。我是否被迫模拟一个实现接口(interface)的具体类?这不会违背

node.js - Nodejs + 要求 instanceof 行为

我在NodeJs中遇到了instanceof的奇怪行为。我正在尝试创建一个引发异常的模块,这些异常可以通过使用模块来捕获并根据异常类型进行处理。然而,测试类型“errorinstanceofmod.MyException”的调用总是返回false。mymodule.js:functionMyException(){}MyException.prototype=newError();MyException.prototype.constructor=MyException;functionFoo(){thrownewMyException();}exports.Foo=Foo;expor

javascript - 在使用来自深度 npm 依赖项的构造函数创建的对象上使用 `instanceof`

背景:我有一个npm模块,其中包含常见的错误处理代码,包括自定义错误:functionCustomError(){/*...*/}CustomError.prototype=Object.create(Error.prototype);CustomError.prototype.constructor=CustomError;module.exports=CustomError;我还有一些其他模块(我们称它们为'module-a'和'module-b'),它们都依赖于错误处理模块。我还有一些使用Bluebirds“过滤捕获”功能的代码:doSomething.catch(CustomE

javascript - 为什么 instanceof 在这里评估为真?

在此代码段中,语句finstanceofPipeWritable返回true(Nodev8.4.0):conststream=require('stream');constfs=require('fs');classPipeWritableextendsstream.Writable{constructor(){super();}}consts=newPipeWritable();constf=fs.createWriteStream('/tmp/test');console.log(finstanceofPipeWritable);//true...???对象s:Object.get

javascript - JavaScript 中 typeof 和 instanceof 的区别

这个问题在这里已经有了答案:instanceofStringnotbehavingasexpectedinGoogleAppsScript(1个回答)关闭9年前。我正在使用node.js,所以这可能是V8特有的。我一直注意到typeof和instanceof之间存在一些奇怪的差异,但这里有一个让我很困扰:varfoo='foo';console.log(typeoffoo);Output:"string"console.log(fooinstanceofString);Output:false那里发生了什么? 最佳答案 typeof

java - 抛出并捕获异常,还是使用instanceof?

我在变量中有异常(未抛出)。最好的选择是什么?Exceptionexception=someObj.getExcp();try{throwexception;}catch(ExceptionExample1e){e.getSomeCustomViolations();}catch(ExceptionExample2e){e.getSomeOtherCustomViolations();}或Exceptionexception=someObj.getExcp();if(exceptioninstanceofExceptionExample1){exception.getSomeCusto