instanceof

typescript MyObject.instanceOf()

匿名 (未验证) 提交于 2019-12-03 08:44:33
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: 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.

How to iterate over a weakmap?

匿名 (未验证) 提交于 2019-12-03 08:39:56
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: A javascript WeakMap ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap ) does not allow you to get the key, or the length or size, by design. Is it possible to nevertheless loop over entries in some way ? If not .. how does the Chrome console do this ? 回答1: Is it possible to nevertheless loop over entries in some way? No, as you say, the contents of a WeakMap are not accessible by design, and there is no iterability. If not … how does the Chrome console do this? The console uses the

Parse a JSON Object With an Undetermined Amount of Child Nodes

匿名 (未验证) 提交于 2019-12-03 08:30:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I working with the Dropbox API and I would like to get the folders and paths from a json object. However I am having problems as there is an undermined number of child nodes. How would I go about parsing something like this? EDIT The JSONObject represents a directory. The top level folder is represented by a json object. Within that object are the sub-folders. Each of them has there down JSONobject, within each of these jsonobject there is an array which represent the subfolders of the subfolder. So I don't know the complete structure of the

Javascript inheritance - instanceof not working?

為{幸葍}努か 提交于 2019-12-03 06:51:19
问题 I'm writing a simple platform game using javascript and html5. I'm using javascript in an OO manner. To get inheritance working i'm using the following; // http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/ function copyPrototype(descendant, parent) { var sConstructor = parent.toString(); var aMatch = sConstructor.match(/\s*function (.*)\(/); if (aMatch != null) { descendant.prototype[aMatch[1]] = parent; } for (var m in parent.prototype) { descendant.prototype[m] = parent

Can I set the type of a Javascript object?

て烟熏妆下的殇ゞ 提交于 2019-12-03 05:59:38
问题 I'm trying to use some of the more advanced OO features of Javascript, following Doug Crawford's "super constructor" pattern. However, I don't know how to set and get types from my objects using Javascript's native type system. Here's how I have it now: function createBicycle(tires) { var that = {}; that.tires = tires; that.toString = function () { return 'Bicycle with ' + tires + ' tires.'; } } How can I set or retrieve the type of my new object? I don't want to create a type attribute if

Java: How to check if an object is an instance of a non-static inner class, regardless of the outer object?

前提是你 提交于 2019-12-03 05:04:54
If I have an inner class e.g. class Outer{ class Inner{} } Is there any way to check if an arbitrary Object is an instance of any Inner , regardless of its outer object? instanceof gives false when the objects are not Inner s from the same Outer . I know a workaround is just to make Inner a static class, but I'm wondering if what I'm asking is possible. Example: class Outer{ Inner inner = new Inner(); class Inner{} public boolean isInner(Object o){ return o instanceof Inner; } } Outer outer1 = new Outer(); Outer outer2 = new Outer(); boolean answer = outer1.isInner(outer2.inner); //gives false

Avoid instanceof in a composite pattern

匿名 (未验证) 提交于 2019-12-03 03:10:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm working on a project for university where we're programming a game. In this game several effects can occur and one effect can affect how another effect behaves. Now one of the idea's was using a composite pattern which seemed like a solid solution at first. The biggest problem here is that the way an effect behaves depends on what effect it's coupled with and the only way we saw to fix this was using .getClass() or instanceof which we want to avoid at all cost. What are some ways to design this issue? Edit (a small example): I don't have

difference between creation unbounded and bounded wild card type array?

匿名 (未验证) 提交于 2019-12-03 03:05:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Why is this code valid ArrayList <?>[] arr = new ArrayList <?>[ 2 ]; but the following two are not? ArrayList <? extends Object >[] arr = new ArrayList <? extends Object >[ 2 ]; ArrayList <? super Object >[] arr = new ArrayList <? super Object >[ 2 ]; The two last rows generate the compile error; error: generic array creation. Please clarify difference. update On the other hand ArrayList<?>[] arr = new ArrayList<?>[2]; compiles good but ArrayList <?> arr = new ArrayList <?>(); not. 回答1: There are a few issues going on here, lets

Java - is there a “subclassof” like instanceof?

匿名 (未验证) 提交于 2019-12-03 03:04:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Im overriding an equals() method and I need to know if the object is an instance of a Event's subclass (Event is the superclass). I want something like "obj subclassof Event". How can this be made? Thanks in advance! 回答1: instanceof can handle that just fine. 回答2: With the following code you can check if an object is a class that extends Event but isn't an Event class instance itself. if(myObject instanceof Event && myObject.getClass() != Event.class) { // then I'm an instance of a subclass of Event, but not Event itself } By default

Instantiating new HTMLElement in TypeScript

匿名 (未验证) 提交于 2019-12-03 03:04:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am trying to instantiate new HTMLDivElement in TypeScript var elem = new HTMLDivElement(); but the browser throws Uncaught TypeError: Illegal constructor. The workaround seems to be to use var elem = document.createElement('div'); but I find this suboptimal for various reasons. Why can't I instantiate DOM elements directly when there is a new keyword for in in the lib.d.ts ? declare var HTMLDivElement: { prototype: HTMLDivElement; new (): HTMLDivElement; } 回答1: Note that the error you get here is "Illegal constructor". This is different