Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I created a JavaScript object, but how I can determine the class of that object?(我创建了一个JavaScript对象,但是如何确定该对象的类呢?)

I want something similar to Java's .getClass() method.(我想要类似于Java的.getClass()方法。)   ask by DNB5brims translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
190 views
Welcome To Ask or Share your Answers For Others

1 Answer

There's no exact counterpart to Java's getClass() in JavaScript.(JavaScript中没有与Java的getClass()完全相同的对象。)

Mostly that's due to JavaScript being a prototype-based language , as opposed to Java being a class-based one.(大多数情况下,这是由于JavaScript是基于原型的语言 ,而不是Java是基于类的 语言 。) Depending on what you need getClass() for, there are several options in JavaScript:(根据您需要getClass()的不同,JavaScript中有几个选项:) typeof instanceof obj. constructor func. prototype , proto .(prototypeproto 。) isPrototypeOf A few examples:(一些例子:) function Foo() {} var foo = new Foo(); typeof Foo; // == "function" typeof foo; // == "object" foo instanceof Foo; // == true foo.constructor.name; // == "Foo" Foo.name // == "Foo" Foo.prototype.isPrototypeOf(foo); // == true Foo.prototype.bar = function (x) {return x+x;}; foo.bar(21); // == 42 Note: if you are compiling your code with Uglify it will change non-global class names.(注意:如果使用Uglify编译代码,它将更改非全局类名。) To prevent this, Uglify has a --mangle param that you can set to false is using gulp or grunt .(为了防止这种情况,Uglify有一个--mangle参数,您可以使用gulpgrunt设置为false。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...