instanceof

JS new.target vs. instanceof

两盒软妹~` 提交于 2019-12-24 05:08:29
问题 So I have done some reading on the new.target boolean added in Node 6.x. Here is a simple example of new.target provided on MDN function Foo() { if (!new.target) throw "Foo() must be called with new"; console.log("Foo instantiated with new"); } Foo(); // throws "Foo() must be called with new" new Foo(); // logs "Foo instantiated with new" But this reads a lot like what I am presently using the below code for var Foo = function (options) { if (!(this instanceof Foo)) { return new Foo(options);

Java - alternative to many else if statements with instanceof conditions

陌路散爱 提交于 2019-12-23 20:49:39
问题 I have this messy code that I really want to clean up because it has about 12 else if's and each of these if statements check for the instanceof 2 objects, so something like: If (parentObj instanceof superclass && parentObj2 instanceof superclass) Each if statement executes something different, so polymorphism I think won't make it any better. Also because 12 different super classes for this little functionality (most if statements execute 1 liners) would be a bit ridiculous. And another

PHP instanceof returns false for true condition

Deadly 提交于 2019-12-23 11:50:05
问题 I'm thoroughly confused as to why php's instanceof operator insists that the LVALUE here is not an instance of the defined class when get_class says that it is. class WhereIn { public function __construct($args) { echo "is instanceof: " . ($args[0] instanceof ActiveRecordField) . EOL; echo "get class: " . get_class($args[0]) . EOL; } } The output from this method is: is instanceof: get class: ActiveRecordField For reference, I'm using PHP 5.6.9. 回答1: If you use namespaces in your code, you

Generics - Legal alternative for (elements instanceof List<? extends Comparable>)

こ雲淡風輕ζ 提交于 2019-12-23 07:50:56
问题 I have this method which unique parameter (List elements) sets elements to a ListModel, but I need to make a validation to see if the generic type implements comparable and since such thing like: if (elements instanceof List<? extends Comparable>) is illegal, I don't know how to do the proper validation. Update I already have done this validation using: (elements.size() > 0 && elements.get(0) instanceof Comparable) but I want to know if is there a cleaner solution for that, using for example

instanceof equivalent in C++ [duplicate]

三世轮回 提交于 2019-12-23 01:41:30
问题 This question already has answers here : C++ equivalent of instanceof (6 answers) Closed 5 years ago . I'm creating a 2D game in C++ that uses levels made out of tiles. The world class has an add(WorldObject* o) function that can both accept a tile or an entity such as an enemy. Both the Tile and the Entity class are derived from WorldObject . In every case, the object should be added to the entities list; but if it is a tile, it should also be added to the tiles list. World.h class World {

原型类型、引用类型

☆樱花仙子☆ 提交于 2019-12-21 11:08:51
原始类型 原始类型的数据都是一些比较简单数据,比如字符串,数字等,比如:true和25,这些数据会被直接存储在变量的内存空间中。ECMAScript 5 给我们提供了5种原始类型: 类型 数据 说明 Boolean true、false 布尔值,true或false Number 12、12.5、NaN 整型、浮点型、特殊值NaN(Not a number) String 'hello'、"hello" 被单引号或双引号扩起来的字符或字符串 Null null 只有一个值null Undefined undefined 只有一个值undefined。任何只声明而没有赋值的变量都会被赋值为undefined。 变量在存储原始类型的数据时,直接将数据存储到变量的内存空间中。当我们将存储原始类型数据的变量赋值给另一个变量时,其实是将变量存储的值复制了一份保存到了另一个变量中。 正因为每一个变量都是使用自己独立的存储空间保存原始类型的数据,因此当我们改变一个变量中的数据时不会影响到另个变量中的数据。 检测原始类型的数据 检测原始类型的数据最好的方式是使用typeof操作符,该操作符会返回一个表示数据类型的字符串。 原始类型数据的方法 虽然字符串,数字,布尔值是原始数据类型,但是也有很多方法可以使用(null和undefined没有方法)。 引用类型 引用类型的数据稍微复杂一点

JavaScript之数组方法整理

做~自己de王妃 提交于 2019-12-21 08:29:06
Array概述 除了Object类型,最常用的类型; 实质:有序的数据列表, 特性:可以动态的调整数组的大小 创建数组的两种方式 构造函数创建方式 var arr = new Array() 数字字面量方式 var arr = [ ]; var arr = [ , , ] //创建3 或 2 项的数组 ---->不同浏览器的解释不一样 不要使用 数组有length属性 ,可读取也可设置 。设置时,如果超出,给超出的部分赋值 undefined , 如果未超出会裁剪数组。 Array 数组方法: 检测数组 instanceof操作符 if (value instanceof Array){ //如果是数组才能进入到这个位置 } 如果一个网页中包含多个框架,数组是从另外一个框架中传入的,那么 instanceof就变得不可靠了 (一般不会出现这种情况) isArray() ECMAScript 5 中为 解决 instanceof 有可能不可靠的问题添加了 isArray()方法 兼容性 IE 9++ 转换方法 和所有对象一样会有 toLcaleString()、toString()、valueOf()方法, toString( ) 返回由数组中的每个值得字符串形式拼接而成的一个以逗号分隔的字符串。 --> 实际上,为了创建这个字符串会调用数组每一项的toString()方法。

How to implement equals with hibernate without risking losing the symmetric property?

北慕城南 提交于 2019-12-21 03:45:34
问题 After reading up on (again, should have done this a long time ago) implementing equals and hashcode correctly i came to these conclusions,that works for me: If pre JDK 7 : Prefer using Apache commons equalsbuilder and hashcodebuilder. (or Guava). Their javadocs contain examples of how to use them in good way. If JDK 7++ : Use the new Objects utility class But, If writing for hibernate some special requistes appear (see sources farther down) Amongst them are the recommended usage of instanceof

比较typeof与instanceof

柔情痞子 提交于 2019-12-20 19:46:56
相同点: JavaScript中typeof和instanceof常用来判断一个变量是否为空,或者是什么类型的。 不同点: typeof的定义和用法: 返回值是一个字符串,用来说明变量的数据类型。 细节: 1)、typeof一般只能返回如下几个结果: number,boolean,string,function,object,undefined。 2)、typeof来获取一个变量是否存在,如if(typeof a!="undefined"){alert("ok")},而不要去使用if(a)因为如果a不存在(未声明)则会出错。 3)、对于Array,Null等特殊对象使用typeof一律返回object,这正是typeof的局限性。 Instanceof定义和用法: instanceof用于判断一个变量是否属于某个对象的实例。 细节: 1)、instanceof返回布尔值true或者false。 2)、var a = new Array(); console.log(a instanceof Array) // true console.log(a instanceof Object) // true a是Array的实例,所以a instanceof Array为true。 但同时Array也是是object 的子类,所以a instanceof Object也为true。 3)

Checking if a class is java.lang.Enum

廉价感情. 提交于 2019-12-20 10:14:34
问题 I'm trying to know if a class is an Enum, but I think I'm missing something: if (test.MyEnum.class instanceof Enum<?>.class) obj = resultWrapper.getEnum(i, test.MyEnum.class); else obj = resultWrapper.getObject(i); It gives me an error saying that Enum.class is not valid. So how I can check if a class is a Enum? I'm pretty sure it is possible to determine that, I'm just unable to get it. Thanks 回答1: The correct syntax would be: Enum.class.isAssignableFrom(test.MyEnum.class) but for enums,