instanceof

Check if a constructor inherits another in ES6

匿名 (未验证) 提交于 2019-12-03 03:03:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have a situation where I need to check if a constructor (X) has another constructor (Y) in its prototype chain (or is Y itself). The quickest means to do this might be (new X()) instanceof Y . That isn't an option in this case because the constructors in question may throw if instantiated without valid arguments. The next approach I've considered is this: const doesInherit = ( A , B ) => { while ( A ) { if ( A === B ) return true ; A = Object . getPrototypeOf ( A ); } return false ; } That works, but I can't shake the sense that

why instanceof does not work with Generic? [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Possible Duplicate: Java: Instanceof and Generics I am trying to write a function which cast a generic List to specific type List. Find the code below public < T > List < T > castCollection ( List srcList , Class < T > clas ){ List < T > list = new ArrayList < T >(); for ( Object obj : srcList ) { if ( obj instanceof T ){ ... } } return list ; } But obj instanceof T showing a compilation error - Cannot perform instanceof check against type parameter T. Use instead its erasure Object >instead since further generic type information

Checking a class type (.class) is equal to some other class type

匿名 (未验证) 提交于 2019-12-03 02:54:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is the following code valid? void myMethod (Class classType) { if (classType == MyClass.class) { // do something } } myMethod (OtherClass.class); If not is there any other approach where I can check if a passed .class (Class Type) is of type - MyClass ? Thanx! 回答1: Yes, that code is valid - if the two classes have been loaded by the same classloader. If you want the two classes to be treated as equal even if they've been loaded by different classloaders, possibly from different locations, based on the fully-qualified name, then just compare

Use CMYK on web page

匿名 (未验证) 提交于 2019-12-03 02:51:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I need to use CMYK colors on my web page. Is there any way to use CMYK in CSS or may be convert CMYK to RGB using JavaScript? EDIT: I mean I have colors creating algorithm in CMYK notation and I need to use it on web page. 回答1: There is no perfect algorithmic way to convert CMYK to RGB. CYMK is a subtractive color system, RGB is an additive color system. Each have different gamuts , which means there are colors that just cannot be represented in the other color system and vice versa. Both are device dependent color spaces, which really means

Android Studio IDE: Break on Exception

匿名 (未验证) 提交于 2019-12-03 02:49:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: It seems my Android Studio does not want to break on any exception by default. Enabling break on "Any Exception" starts breaking within actual JDE libraries. Is there any way to force it to break only on exceptions within my code only? Coming from Visual Studio universe, looking for the default VS debug behavior here. 回答1: To break on all exceptions, caught or uncaught: Open the Breakpoints window via Run -> View Breakpoints . The Breakpoints dialog appears. In the left pane, scroll to the bottom. Select Any exception under Java Exception

How to test whether method return type matches List&lt;String&gt;

匿名 (未验证) 提交于 2019-12-03 02:49:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: What is the easiest way to test (using reflection), whether given method (i.e. java.lang.Method instance) has a return type, which can be safely casted to List<String>? Consider this snippet: public static class StringList extends ArrayList<String> {} public List<String> method1(); public ArrayList<String> method2(); public StringList method3(); All methods 1, 2, 3 fulfill the requirement. It's quite easy to test it for the method1 (via getGenericReturnType(), which returns instance of ParameterizedType), but for methods2 and 3, it's not so

Is the 'isa' variable in objective-C equal to 'instanceof' in Java

匿名 (未验证) 提交于 2019-12-03 02:47:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Simple question: is it fair to say that the 'isa' instance variable in Objective-C provides the same functionality as the 'instanceof' operator in Java? 回答1: These are different concepts. One is a member of a struct, while another is an operator . To mimic a strict interpretation of the instanceof operator in Java, you can do pointer comparison on the isa member: if ( obj -> isa == [ SomeClass class ]) { //obj is an instance of SomeClass } But it is recommended that you use the NSObject protocol's -isMemberOfClass: method to

Difference between using Array.isArray and instanceof Array

匿名 (未验证) 提交于 2019-12-03 02:45:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: There is two ways to figure out if an array is an array or an object. Using typeof item === "object"; will return true for an object and an array since arrays are relatively new to javascript and arrays are prototypes of objects(may have worded this wrong, feel free to correct me). So the two ways I know of to determine if an Array is an Array are: Solution 1: Array.isArray(item); Solution 2: item instanceof Array; My questions are: What is the difference between these two solutions? Which of these two is the preferred solution? Which has a

JavaFX: Initial value of bidirectional binding

匿名 (未验证) 提交于 2019-12-03 02:31:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: What happens when I do binding for these two properties? ObjectProperty<Object> propertyA = new SimpleObjectProperty<>(); ObjectProperty<Object> propertyB = new SimpleObjectProperty<>(); propertyA.set(new ObjectA()); propertyB.set(new ObjectB()); Bindings.bindBidirectional(propertyA, propertyB); If both properties are supposed to hold the same object reference, then after this binding, would both properties be holding the reference of ObjectA or ObjectB ? 回答1: When you call: Bindings.bindBidirectional(propertyA, propertyB); The value of

The difference between “instanceof List” and &#039;o instanceof List&lt;?&gt;\&quot;

匿名 (未验证) 提交于 2019-12-03 02:31:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I don't see any difference in the following: Object o = new LinkedList<Long>(); System.out.println(o instanceof List); System.out.println(o instanceof List<?>); Is there any practical use of instanceof List<?> when instanceof List can't be used instead and vise versa? 回答1: No difference. The wildcard is erased at compile time. 回答2: According to this blog the answer is 'they are exactly the same': as javac forbids instanceof expressions whose target type is a generic type; for casts, the compiler is slightly more permissive since casts to