instanceof

JS实现逼真的雪花飘落特效

守給你的承諾、 提交于 2019-12-18 11:41:15
逼真的雪花飘落特效 效果图: 图片素材 : --> ParticleSmoke.png 代码如下,复制即可使用: <!doctype html> <html> <head> <meta charset="UTF-8"> <meta name="renderer" content="webkit"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>JS实现逼真的雪花飘落特效</title> <!-- 此处需要自己修改JS路径 --> <script type="text/javascript" src="css/ThreeCanvas.js"></script> <script type="text/javascript" src="css/snow3d.js"></script> <script type="text/javascript" src="css/snow.js"></script> <style type="text/css">body{background-color:#000044;margin:0px;overflow

Passing a class as an argument to a method in java

假如想象 提交于 2019-12-18 07:40:10
问题 I am writing a method were I would like to pass a class to a method, where a part of the code includes checking if the object is of a certain type. This is what I want (but which obviously doesn't work): private static class MyClass1 { /***/ } private static class MyClass2 { /***/ } private void someFunc() { /* some code */ methodName(MyClass1); methodName(MyClass2); } private void methodName(Class myClass) { Object obj; /* Complicated code to find obj in datastructure */ if (obj instanceof

How to find type without using instanceof?

我只是一个虾纸丫 提交于 2019-12-18 07:02:16
问题 I have a List of interface type Criteria within my class Query . List<Criteria> criteria = new ArrayList<Criteria>(); I have several concrete implementations of Criteria . I want to give Query a method that iterates through my criteria list, and depending on the concrete type, execute some logic. I'm presently doing this with instanceof like so: for(Criteria c : criteria) { if(c instanceof ContextualCriteria){ // logic } ... } Is this the only/best way? 回答1: Does the logic sensibly belong in

instanceof HTMLElement in IFRAME is not Element or Object?

喜夏-厌秋 提交于 2019-12-18 04:08:17
问题 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

判断javascript的数据类型

情到浓时终转凉″ 提交于 2019-12-18 03:29:07
1. typeof typeof 操作符返回变量或表达式的类型。 语法: typeof arr; 这种数据类型检测只对简单数据类型适用,对数组和对象不实用. 2. instanceof instanceof运算符用来判断一个构造函数的prototype属性所指向的对象是否存在另外一个要检测对象的原型链上 语法: obj instanceof Object; 这种方式对检测对象的实例比较好用,但是还是区分不了数组和对象. 3. constructor constructor 属性返回对创建此对象的数组函数的引用。 语法: object.constructor 'str' . constructor === String 4. Object.prototype.toString.apply()或者Object.prototype.toString.call() 使用 toString() 方法返回一个表示该对象的字符串,每个对象都有一个 toString() 方法, toString() 方法被每个 Object 对象继承。为了每个对象都能通过 Object.prototype.toString() 来检测,需要以 Function.prototype.call() 或者 Function.prototype.apply() 的形式来调用,传递要检查的对象作为第一个参数,称为

instanceof operator in java for comparing different classes

醉酒当歌 提交于 2019-12-17 20:08:37
问题 I was trying to see how instanceof operator in Java works and am facing a very odd issue. public static void main(String[] args) { Map m = new HashMap(); System.out.println("m instanceof Date: " + (m instanceof Date)); } The above returns false as expected. However, public static void main(String[] args) { HashMap m = new HashMap(); System.out.println("m instanceof Date: " + (m instanceof Date)); } This does not even compile. I get an error inconvertible types found : java.util.HashMap

What is the best way to check if an object is an array or not in Javascript?

…衆ロ難τιáo~ 提交于 2019-12-17 19:05:57
问题 Say I have a function like so: function foo(bar) { if (bar > 1) { return [1,2,3]; } else { return 1; } } And say I call foo(1) , how do I know it returns an array or not? 回答1: I use this function: function isArray(obj) { return Object.prototype.toString.call(obj) === '[object Array]'; } Is the way that jQuery.isArray is implemented. Check this article: isArray: Why is it so bloody hard to get right? 回答2: if(foo(1) instanceof Array) // You have an Array else // You don't Update: I have to

原型和原型链

限于喜欢 提交于 2019-12-17 18:22:17
一.构造函数 1.构造函数最好大写开头 构造函数末尾一行默认有return this,故可理解为加了this为公用属性,没加this为私有属性。 1 function F(){ 2 3 this.name = "chen"; 4 5 age = 22; 6 7 //默认有return this 8 9 } 10 11 var f = new F(); 12 13 console.log(f.age)//undefined 14 15 console.log(f.name)//"chen" 2.new 一个对象的过程 1.把参数传进去 2.函数里面的this先变成一个空对象 3.属性赋值 4.默认把this return出来 5.把this赋值给f 3.从易读性和性能上考虑,都选择采用前面的写法 var a = {}是var a = new Object()的语法糖 var a = []是var a = new Array()的语法糖 var function a(){} 是 var a = new Function()的语法糖 使用instanceof判断一个函数是否是一个变量的构造函数 Functtion()构造函数的前n个为传入的参数,最后一个为函数体。参数都为字符串。 1 new Function("a", "b", "c", "return a+b+c") 2 3 new

Avoiding 'instanceof' in Java

萝らか妹 提交于 2019-12-17 17:28:35
问题 I have the following (maybe common) problem and it absolutely puzzles me at the moment: There are a couple of generated event objects which extends the abstract class Event and I want to divide them to Session Beans, like public void divideEvent(Event event) { if (event instanceof DocumentEvent) { documentGenerator.gerenateDocument(event); } else if (event instanceof MailEvent) { deliveryManager.deliverMail(event); ... } ... } But there could be more than two event types in future, so the if

Java instanceof operator

偶尔善良 提交于 2019-12-17 16:52:58
问题 Is there a valid class Type variable that can be used with the instanceof operator? For Example: String s = "abc"; Class<?> classType = String.class; if (s instanceof classType) { //do something } as an alternative to this: if (s.getClass() == classType) { //do something } Would there be any performance benefit? 回答1: What you're doing is not actually the same. Consider what happens with subclasses (I know you can't subclass String , so in the String case it doesn't matter). class A {} class B