在我们开始之前,需要记住和注意一些重要的语句(对于所有字符串文字/基元、字符串对象、数字文字/基元、数字对象等都是如此):
- JavaScript 中的所有对象都继承自
Object,并从 Object.prototype – String、Number 等继承方法和属性(很像 Java)。
- JS 有 6 种基本类型——string、number、boolean、null、undefined 和 符号
- JS 有它们对应的包装对象——
String、Number、Boolean 和 Symbol
- 正如您在上面看到的,JS 有字符串作为原语以及
Object
- 原语不是
Object 类型。
- 字符串字面量是原语,
String 对象的类型为 Object。
-
instanceof 运算符测试对象的原型链中是否具有构造函数的原型属性。 (这里获得 TRUE 的第一个条件是 instanceof 应该用于对象或其子类)
-
typeof 运算符返回一个字符串,指示未计算的操作数的类型。
字符串作为原语:
字符串原语或文字可以通过以下方式构造:
var str1 = “Hello”;
var str2 = String(“Hello”);
typeof str1; //Output = "string"
typeof str2; //Output = "string"
str1 instanceof (String || Object) //Output = false because it is a primitive not object
str2 instanceof (String || Object) //Output = false because it is a primitive not object
字符串作为对象:
可以通过从新对象调用其构造函数来构造字符串对象:
var str3 = new String(“Hello”);
typeof str3; //Output = "string"
str3 instanceof (String) //Output = true because it is a String object
str3 instanceof (Object) //Output = true because it is an Object
以上所有内容可能看起来不太明显,但有必要奠定基础。
现在,让我谈谈你的情况。
Object.prototype.isString = function() {
return typeof this === "string";
}
"5".isString() //returns false
"".isString() //returns false
var str = "string"
str.isString() //returns false
由于称为自动装箱的概念,您将 FALSE 作为 o/p。当您在字符串文字上调用任何方法时,它会被转换为字符串对象。 Read this 来自 MSDN -“字符串文字的方法”,请自行确定。
因此,在您的原型中,当您使用 typeof 检查类型时,它永远不会是文字 (typeof == "string"),因为它已经被转换为对象。这就是您这样做的原因假的,如果你检查typeof 是否有object,那么你会得到真,我将在下面详细讨论:
-
typeof 将提供有关实体类型的信息 - 对象或原语(字符串、数字等)或函数。
-
instanceof 将提供有关它是什么类型的 JS 对象的信息 - 对象或字符串或数字或布尔值或符号
现在让我谈谈提供给您的解决方案。它说要进行instanceof 检查,这是 100% 正确的,但请注意,在到达您的原型时,它可能是对象类型或函数类型。因此,我在下面提供的解决方案将为您提供相同的图片。
我的建议是有一个通用函数,它会返回实例的类型,然后你可以根据它是数字还是字符串等做任何你想做的事情。isString 很好,但你必须写isNumber 等等,所以只有一个函数可以返回实例的类型,甚至可以处理函数类型。
以下是我的解决方案:
Object.prototype.getInstanceType = function() {
console.log(this.valueOf());
console.log(typeof this);
if(typeof this == "object"){
if(this instanceof String){
return "String";
} else if(this instanceof Boolean){
return "Boolean";
} else if(this instanceof Number){
return "Number";
} else if(this instanceof Symbol){
return "Symbol";
} else{
return "object or array"; //JSON type object (not Object) and array
}
} else if(typeof this == "function"){
return "Function";
} else{
//This should never happen, as per my knowledge, glad if folks would like to add...
return "Nothing at all";
}
}
输出:
new String("Hello").getInstanceType() //String
"Hello".getInstanceType() //String
(5).getInstanceType() //Number
(true).getInstanceType() //Boolean
Symbol().getInstanceType() //Symbol
var ddd = function(){}
var obj = {}
obj.getInstanceType() //object or array
var arr = []
arr.getInstanceType() //object or array
ddd.getInstanceType() //Function
($).getInstanceType() //Function, because without double quotes, $ will treated as a function
("$").getInstanceType() //String, since it came in double quotes, it became a String
总结一下:您的 2 个问题如下
但我需要做的是在任何值上使用它,并访问
函数中的值。
您可以使用this 访问函数中的值。在我的解决方案中你可以看到console.log(this.valueOf());
是否有任何解决方法可以让它成为任何对象的属性,如果
我使用 Object.prototype?
您可以按照上述解决方案从Object.prototype.getInstanceType 实现它,并且您可以在任何有效的 JS 对象上调用它,您将获得所需的信息。
希望这会有所帮助!