javascript 执行 `typeof JSON` 比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11284877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:38:22  来源:igfitidea点击:

Perform a `typeof JSON` comparison

javascriptjsoncoffeescript

提问by noncom

I have a method that receives something and it needs to determine the type of the received value. I can use the typeofthing to perform regular comparisons like if it is a number or a string. But how can I do this for JSON objects? Comparing them with JSONbrings up the error:

我有一个接收某些东西的方法,它需要确定接收到的值的类型。我可以使用它typeof来执行常规比较,例如它是数字还是字符串。但是我怎样才能为 JSON 对象做到这一点呢?将它们与它们进行比较JSON会出现错误:

Uncaught TypeError: Expecting a function in instanceof check, but got #< Object>

未捕获的类型错误:在 instanceof 检查中需要一个函数,但得到了 #< Object>

So I guess that comparing a JSON object type with JSONis not the way?

所以我猜比较 JSON 对象类型JSON不是这样的吗?

The original code is like:

原来的代码是这样的:

check = (what) ->
  if what instanceof JSON
    alert "Yooo"

check({compare: "me"})

回答by sachleen

The type will be object, not JSON. To see what you're working with, you can check if it has the properties you're looking for. Check the length, or if it has specific keys.

类型将是object,不是JSON。要查看您正在使用的内容,您可以检查它是否具有您正在寻找的属性。检查长度,或者它是否有特定的键。

Here's a pretty good informational page on working with JSON. JSON in JavaScript

这是一个关于使用 JSON 的非常好的信息页面。JavaScript 中的 JSON

回答by Raab

first type ofreturn string, second, there is no such thing as Type JSONin type ofpossible return values. see the this page for detail

第一个type of返回字符串,第二个,JSONtype of可能的返回值中没有 Type 这样的东西。详情请参阅此页面

in your case you will receive "object"

在您的情况下,您将收到 "object"

回答by Rune FS

JSON stands for JavaScript Object notation and is simply a name for how object literals are written in JavaScript it is not a type.

JSON 代表 JavaScript 对象表示法,它只是在 JavaScript 中编写对象文字的方式的名称,它不是一种类型。

var a = {"foo":"My foo","bar" : 4};
var b = {"foo":"My foo","bar" : 0};
var c = {"foo":"My c foo","barella" : -1};
var d = '{"baz":"My baz","bar" : 4}';

a,b,c and d are all objects the first three of type object the fourth of type string. You could from a type theoretic point of view say that the first two have the same type. If you did eval("var e =" + d)then the string would be in d would be evaluated and since d is an object serialized to JSON the result would be a valid object that would be assigned to e.

a,b,c 和 d 都是对象类型的前三个对象,类型字符串的第四个。从类型理论的角度来看,您可以说前两个具有相同的类型。如果您这样做了,eval("var e =" + d)那么 d 中的字符串将被评估,并且由于 d 是一个序列化为 JSON 的对象,因此结果将是一个有效的对象,该对象将分配给 e。

in other words JSON is no type it's part of the JavaScript grammar, the result of evaluating JSON is an object and the type of that object will vary depending on the object literal. using typeofon such an object will yield "object" (regards less of the type theoretic type of the object).

换句话说,JSON 不是类型,它是 JavaScript 语法的一部分,评估 JSON 的结果是一个对象,该对象的类型将根据对象字面量而有所不同。使用typeof这样一个对象上会产生“对象”(问候类型理论类型的对象的更小)。

If you wish to test and object against a specific type you would therefor have to test it for all the expected properties and methods

如果您希望针对特定类型进行测试和反对,则必须针对所有预期的属性和方法对其进行测试

回答by SMathew

class JSON
   constructor: (@data) ->

   get: (key) ->
     @data[key]

   set: (key, value) ->
     @data[key] = value

a = new JSON "foo":"My foo", "bar" : 4

a.get('foo')
a.data.foo
a.data['foo']

console.log(a instanceof JSON)

:D You really shouldn't be doing this though, at least not to create a JSON type. But it's possible that you can create your own wrapper for pretty much anything. Combining this with the Object.defineProperty to setup getters and setters based on @data, you could do some powerful stuff. It doesn't have method_missing methods, but you can achieve similar results with Object.defineProperty

:D 你真的不应该这样做,至少不要创建一个 JSON 类型。但是您可以为几乎任何东西创建自己的包装器。将其与 Object.defineProperty 结合使用以设置基于 @data 的 getter 和 setter,您可以做一些强大的事情。它没有 method_missing 方法,但您可以使用 Object.defineProperty 获得类似的结果