如何检查值是否是JavaScript中的对象?
在我的特殊情况下:
callback instanceof Function
Run Code Online (Sandbox Code Playgroud)
要么
typeof callback == "function"
Run Code Online (Sandbox Code Playgroud)
甚至重要,有什么区别?
额外资源:
JavaScript-Garden typeof vs instanceof
在ES6中,如果我创建一个类并创建该类的对象,我该如何检查该对象是否是该类?
我不能只使用,typeof因为对象仍然是"object".我只是比较构造函数吗?
例:
class Person {
constructor() {}
}
var person = new Person();
if ( /* what do I put here to check if person is a Person? */ ) {
// do stuff
}
Run Code Online (Sandbox Code Playgroud) 在JavaScript中,我可以通过以下方式声明字符串;
var a = "Hello World";
var b = new String("Hello World");
Run Code Online (Sandbox Code Playgroud)
但是a不是String的实例......
console.log(a instanceof String); //false;
console.log(b instanceof String); //true;
Run Code Online (Sandbox Code Playgroud)
那么如何找到类型或" instanceof"字符串文字?
可以强制JavaScript new String()为每个字符串文字创建一个?
这里发生了什么?正当我认为我内外都知道JS时,这个宝石出现了.
String.prototype.doNothing = function() {
return this;
};
alert(typeof 'foo'.doNothing()) // object
alert(typeof 'foo') // string
Run Code Online (Sandbox Code Playgroud)
这打破了一些期望字符串的东西,比如jQuery的.text(str)方法.
我正在使用node.js,所以这可能是V8特有的.
我总是注意到typeof和instanceof之间存在一些奇怪的差异,但这里有一个让我烦恼的事:
var foo = 'foo';
console.log(typeof foo);
Output: "string"
console.log(foo instanceof String);
Output: false
Run Code Online (Sandbox Code Playgroud)
那里发生了什么?
我像这样扩展Object:
Object.prototype.is_a = function (x) {
return this instanceof x;
}
Run Code Online (Sandbox Code Playgroud)
一切都按预期工作
"foo".is_a(String) // true
"foo".is_a(Object) // true
"foo".is_a(Array) // false
"foo".is_a(Function) // false
"foo".is_a(Boolean) // false
"foo".is_a(Date) // false
"foo".is_a(Number) // false
"foo".is_a(RegExp) // false
Run Code Online (Sandbox Code Playgroud)
但当
"foo" instanceof String // false
Run Code Online (Sandbox Code Playgroud)
thisis_a()功能上的关键字是foo对的吗?为什么它会返回不同的结果?
以下表示表达式"true instanceof Boolean"的计算结果为false.为什么这个表达式会被评估为false?
$(document).ready(function() {
var $result = $('#result');
if(true instanceof Boolean) {
$result.append('I\'m a Boolean!');
} else {
$result.append('I\'m something other than a Boolean!');
}
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="result"></div>Run Code Online (Sandbox Code Playgroud)
为什么以下两行会返回不同的结果?
("test" instanceof String) // returns false
("test".constructor == String) // returns true
Run Code Online (Sandbox Code Playgroud)
在镀铬版本28.0.1500.95 m的控制台中测试
对于本机类型,它的工作方式有所不同吗
我目前正在学习javascript,有一些我不明白的东西.
//This means that I am using a method from the String.prototype
"ThisIsMyString".length
Run Code Online (Sandbox Code Playgroud)
所以,如果我使用("ThisIsMyString"instanceof String)假设返回true,不是吗?但事实证明,返回false ..我相信这是因为原始类型.
这是我的问题:如果"ThisIsMyString"不是String的实例,它如何从该Object访问属性?我不知道这个难题的一部分是什么?
试图让我的JavaSscript基础变得强大.所以问题是关于字符串文字.不是Objects吗?如果你的回答是'是'那么我的问题是为什么要instanceof回来false?
> var s = new String
> s.constructor.toString()
function String() { [native code] }
> typeof s
object
> s instanceof String
true
> s instanceof Object
true
> s instanceof Number
false
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.
> typeof 'c'
string
> 'c' instanceof Object
false
> 'c' instanceof String
false
> 'c'.length
1
> 'c'.charAt(0)
c
> 'c'.constructor.toString()
function String() { [native code] }
Run Code Online (Sandbox Code Playgroud) 在Javascript中,我们可以直接调用字符串文字的方法而不将其括在圆括号内.但不适用于其他类型,如数字或函数.这是一个语法错误,但有没有理由为什么Javascript词法分析器需要将这些其他类型括在圆括号中?
例如,如果我们使用alert方法扩展Number,String和Function并尝试在文字上调用此方法,则它是Number和Function的SyntaxError,而它适用于String.
function alertValue() {
alert(this);
}
Number.prototype.alert = alertValue;
String.prototype.alert = alertValue;
Function.prototype.alert = alertValue;
Run Code Online (Sandbox Code Playgroud)
我们可以直接在字符串对象上调用alert:
"someStringLiteral".alert() // alerts someStringLiteral
Run Code Online (Sandbox Code Playgroud)
但它是关于数字和函数的SyntaxError.
7.alert();
function() {}.alert();
Run Code Online (Sandbox Code Playgroud)
要使用这些类型,我们必须将其括在括号内:
(7).alert(); // alerts "7"
(function() {}).alert(); // alerts "function() {}"
Run Code Online (Sandbox Code Playgroud)
更新:
@Crescent的链接和@Dav和@Timothy的答案解释了为什么7.alert()失败,因为它正在寻找一个数值常量,并且为了超越它,插入额外的空格或额外的点.
7 .alert()
7..alert()
7. .alert();
Run Code Online (Sandbox Code Playgroud)
是否有类似的语法原因,为什么在调用方法之前需要将函数括在括号中?
我不熟悉解释器和词法分析器,知道它是否可以通过某种先行的方式解决,因为Ruby是一种动态语言并且可以解决这个问题.例如:-
7.times { |i| print i }
Run Code Online (Sandbox Code Playgroud)
更新2:
@ CMS的答案一直在理解为什么功能不起作用.以下陈述有效:
// comma operator forces evaluation of the function
// alerts "function() {}"
<any literal>, function() {}.alert();???????
// all examples below are forced …Run Code Online (Sandbox Code Playgroud) javascript ×12
instanceof ×3
syntax ×2
typeof ×2
ecmascript-6 ×1
lexer ×1
methods ×1
node.js ×1
object ×1
string ×1
types ×1