instanceof

Alternative to instanceof?

人盡茶涼 提交于 2019-11-30 19:46:51
问题 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

怎样理解undefined和 null

最后都变了- 提交于 2019-11-30 18:28:01
前言: undefined表示 "未定义", null 表示 "空" 第一步: 一般在变量或属性没有声明或者声明以后没有赋值时, 这个变量的值就是undefined; typeof a; // "undefined"; var b; typeof(b); // "undefined"; var c = {}; typeof(c.name); // "undefined"; 第二步: null是检测变量指向的内存地址是否存在, 即: 如果变量不指向任何一个内存地址, 则返回null. document.getElementById("fasdf") // null Object.prototype.__proto__; // null 注意: null 和 undefined 都是基本类型, 使用instanceof检测类型时都会返回false; 但null非常特殊, 使用typeof去判断时会返回object, 但是有instanceof判断时又会返回false; 一般判断某个变量是否为null, 使用的是: null === null; null instanceof Object; // false undefined instanceof Object; // false typeof null; // "object"; typeof undefined; //

怎样检测数据类型

有些话、适合烂在心里 提交于 2019-11-30 18:23:24
前言: 判断基本类型用: typeof, 判断引用类型用: instanceof 注意: 1. typeof可以当关键字使用, 也可以当函数使用, 它可以检测基本类型, 但无法检测引用类型. 2. instanceof 只能作关键字使用, 可以检测引用类型, 不能检测基本类型. 第一步: typeof 的使用方法 typeof 1; // "number"; typeof true; // "boolean"; typeof "hello"; // "string"; typeof []; // "object"; typeof console; // "object"; typeof {}; // "object" 注意: 上面的代码告诉我们, typeof只能检测基本类型, 不能检测引用类型(全返回object); 第二步: instanceof 的使用方法 instanceof 可以检测引用类型, 其实际含义是: 检测某个实例是否是某个构造函数的实例 console.log([] instanceof Array); // true; console.log({} instanceof Object); // true; console.log([] instanceof Object); // true; console.log(console.log instanceof

instanceof operator fails when passing an object through windows

自闭症网瘾萝莉.ら 提交于 2019-11-30 17:49: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"

Why getClass returns the name of the class + $1 (or $*)

丶灬走出姿态 提交于 2019-11-30 13:37:49
I'm writing a piece of code in which I have to cast an Object if it is an instance of a certain class. As usual I'm using instanceof for checking the compatibility. The problem is that the check is never satisfied because the objects belong to "strange" classes. For example; when I call the method getClass().getSimpleName() on this object it return me the name of the class + $* (e.g. ViewPart$1 instead of ViewPart ). What does this $* means? Is there a solution or a workaround? That shows an inner class (either anonymous (if it has a number) or named). For example: class Foo { static class Bar

GWT work-around for missing Class.isInstance()

好久不见. 提交于 2019-11-30 13:10:59
I'm trying to write a job scheduling system in GWT that maintains an array of exceptions ( Class<? extends Exception>[] exceptions ), that might be resolved by retrying the job. For this, if the scheduler catches an exception, I need to see if this exception matches one of the classes in the array. So, I would like to have a function like this: boolean offerRetry(Exception exception) { for (Class<? extends Exception> e: exceptions) if (e.isInstance(exception)) return true; return false; } Unfortunately Class.isInstance(...) isn't available in GWT. Is there a good work-around for this? My

instanceof HTMLElement in IFRAME is not Element or Object?

大城市里の小女人 提交于 2019-11-30 13:01:55
Trying determinate DOM Element by simple check isElement = SomeThing instanceof Element works in main document, but not on (all?) nodes in iframe. Example output (Google Chrome): (mdiv is DIV in main document, idiv is DIV in iframe) OMGWTF ok: mdiv instanceof Element ... true ... [object HTMLDivElement] ok: mdiv instanceof Object ... true ... [object HTMLDivElement] ko: idiv instanceof Element ... false ... [object HTMLDivElement] KO : idiv instanceof Object ... false ... [object HTMLDivElement] There are different javascript implementations for main document and for iframe documents ???

typescript MyObject.instanceOf()

北城以北 提交于 2019-11-30 12:01:08
All of our typescript classes inherit (directly or indirectly) from: export class WrObject { className:string; public instanceOf(name : String) : boolean { return this.className === name; } } We then declare a subclass as: export class Section extends WrObject { public static CLASS_NAME = 'Section'; className = Section.CLASS_NAME; public instanceOf(name : String) : boolean { if (this.className === name) return true; return super.instanceOf(name); } } And you can then check with: if (obj.instanceOf(Section.CLASS_NAME)) It all works great. However, I think it would be cleaner if we could do: if

OkGo二次封装工具类

て烟熏妆下的殇ゞ 提交于 2019-11-30 12:00:46
public class OkGoUtils { /** * 必须在Application中初始化 * @param context Application对象 * @author LH * created at 2019/9/25 10:23 */ public static void init(Application context){ OkHttpClient.Builder builder = new OkHttpClient.Builder(); HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo"); loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY); //log打印级别,决定了log显示的详细程度 loggingInterceptor.setColorLevel(Level.INFO); //log颜色级别,决定了log在控制台显示的颜色 builder.addInterceptor(loggingInterceptor); //添加OkGo默认debug日志 builder.readTimeout(10000, TimeUnit.MILLISECONDS); //全局的读取超时时间

通过注解方式导入导出execl

三世轮回 提交于 2019-11-30 11:53:02
自定义注解@ExcelField package cn.lisongyu.common.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @author lisongyu * @date 2019/09/23 */ @Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface ExcelField { /** * 导出字段名(默认调用当前字段的“get”方法,如指定导出字段为对象,请填写“对象名.对象属性”,例:“area.name”、“office.name”) */ String value() default ""; /** * 导出字段标题(需要添加批注请用“**”分隔,标题**批注,仅对导出模板有效) */ String title(); /** * 导出字段对齐方式(0:自动;1:靠左;2