instanceof

Why are JavaScript primitives not instanceof Object?

隐身守侯 提交于 2019-12-01 03:58:44
Today I happened to have too much time to kill and I played a bit with the Node (v0.10.13) command line: > 1 instanceof Object false > (1).__proto__ {} > (1).__proto__ instanceof Object true > (1).__proto__.__proto__ === Object.prototype true Now, according to MDN , what instanceof does is: The instanceof operator tests whether an object has in its prototype chain the prototype property of a constructor. But clearly Object.prototype IS in 1 's prototype chain. So why is 1 instanceof Object false? Perhaps because 1 is a primitive not an object to begin with? Okay, I accept that, and I did more

TypeScript instanceof not working

拈花ヽ惹草 提交于 2019-12-01 02:12:46
问题 I'm having issues using the instanceof operator and it doesn't seem to work. Here is a part of my code: const results = _.map(items, function(item: Goal|Note|Task, index: number) { let result = {}; if (item instanceof Goal) { result = { id: index, title: item.name }; } else if (item instanceof Note) { result = { id: index, title: item.content.text }; } else if (item instanceof Task) { result = { id: index, title: item.name }; } console.log(item); console.log(item instanceof Goal); console.log

In Java, can I use a primitive type literal or type variable in an instanceof expression?

北城余情 提交于 2019-12-01 01:03:55
Can I use a primitive type literal or type variable in an instanceof expression? class MyClass<T> { { boolean b1 = null instanceof T; // T erasure -> Object should be used boolean b2 = 2 instanceof Integer; // Incompatible operands } I'm getting compilation errors. Is there any way to circumvent these errors and use a primitive type literal/type variable in an instanceof expression? Basically, I want to be reassured that no, I will never be able to do that. Nope, because of type erasure . An instance of MyClass<T> doesn't actually know what T is. You need to have an instance of Class<T> . Then

Alternative to instanceof?

扶醉桌前 提交于 2019-12-01 00:57:50
I've heard that it is bad design to use instanceof or equivalent ( http://www.javapractices.com/topic/TopicAction.do?Id=31 , when should we use instanceof and when not ) which I can agree on, mainly because it can make the code hard to reuse. However, in some cases I've found it hard to come up with a good alternative to instanceof. For example, say that I want to make a Real-Time-Strategy game. The game consists of obstacles, buildings and tanks, all placed on a grid and each entitity takes up exactly one unit in the grid. So I create the class Entity which is the superclass of the classes

instanceof operator - why there is Illegal compile time error

前提是你 提交于 2019-11-30 23:38:18
Considering the following code, I don't understand why "System.out.println( c2 instanceof D);" will result an "illegal compile time error" but not return "false"? Many thanks for your help! interface I { } class A { int x = 1;} class B extends A implements I { int y = 2;} class C extends B { } class D extends B{ } class E implements I { } C c2 = new C();` The error from Java 8 is: error: incompatible types: C cannot be converted to D And indeed, C and D are not in the same lineage (other than both being Object ). Since the compiler can tell you at compilation time that the instanceof will

instanceof operator fails when passing an object through windows

别等时光非礼了梦想. 提交于 2019-11-30 22:36:00
In order to pass data between windows, I open new windows via the window.open method and set a property of the newly opened window to an object. This allows me not only to pass data, but to share the instance of the variable, meaning if I modify the object, or any of its derived properties, on one window, it modifies it on all windows. The problem, however, is something is going very funny with the instanceof operator. When I do typeof m m instanceof Object The first line returns "object" while the second one returns false . I specifically need the instanceof operator to check between arrays

Check ArrayList for instance of object

落花浮王杯 提交于 2019-11-30 21:18:47
I have a java method that should check through an ArrayList and check if it contains an instance of a given class. I need to pass the method the type of class to check for as a parameter, and if the List contains an object of the given type, then return it. Is this achievable? public static <T> T find(Collection<?> arrayList, Class<T> clazz) { for(Object o : arrayList) { if (o != null && o.getClass() == clazz) { return clazz.cast(o); } } return null; } and call String match = find(myArrayList, String.class); public static <T> T getFirstElementOfTypeIn( List<?> list, Class<T> clazz ) { for (

Why does this instanceof code work and does not cause a compile time error?

元气小坏坏 提交于 2019-11-30 20:53:17
In the following code, the type of x is I (although x also implements J but thats not known at compile time) so why is it that the code at (1) doesn't result in a compile time error. Because at compile time only the type of the reference is considered. public class MyClass { public static void main(String[] args) { I x = new D(); if (x instanceof J) //(1) System.out.println("J"); } } interface I {} interface J {} class C implements I {} class D extends C implements J {} instanceof is used used for runtime determination of an object's type. You are trying to determine if x is really an object

Why “t instanceof T” is not allowed where T is a type parameter and t is a variable?

只谈情不闲聊 提交于 2019-11-30 20:36:26
Eclipse says that the instanceof operation is not allowed with Type Parameter due to generic type eraser. I agree that at runtime, no type information stays. But consider the following generic declaration of class : class SomeClass<T>{ T t; SomeClass(Object o){ System.out.println(o instanceof T); // Illegal } } At runtime, no T would be present! But if I instantiate this class of type Integer, then the corresponding object will have a field t of type Integer. Then, why can't I check the type of a variable with T which can be replaced by Integer at runtime. And I would be actually doing

In Java, can I use a primitive type literal or type variable in an instanceof expression?

六眼飞鱼酱① 提交于 2019-11-30 20:06:41
问题 Can I use a primitive type literal or type variable in an instanceof expression? class MyClass<T> { { boolean b1 = null instanceof T; // T erasure -> Object should be used boolean b2 = 2 instanceof Integer; // Incompatible operands } I'm getting compilation errors. Is there any way to circumvent these errors and use a primitive type literal/type variable in an instanceof expression? Basically, I want to be reassured that no, I will never be able to do that. 回答1: Nope, because of type erasure