java与js的instanceof与typeof的使用

java与js的instanceof与typeof的使用

instanceof 用来测试一个对象是否为一个类的实例,能不能转为某个类的对象。用法为:

boolean result = obj instanceof Class
其中 obj 为一个对象,Class 表示一个类或者一个接口,当 obj 为 Class 的对象,或者是其直接或间接子类,或者是其接口的实现类,结果result 都返回 true,否则返回false。

注意:编译器会检查 obj 是否能转换成右边的class类型,如果不能转换则直接报错,如果不能确定类型,则通过编译,具体看运行时定。

obj 必须为引用类型,不能是基本类型

int i = 0;
System.out.println(i instanceof Integer);//编译不通过
System.out.println(i instanceof Object);//编译不通过
instanceof 运算符只能用作对象的判断。

obj 为 class 类的直接或间接子类

public class Person {

}
public class Man extends Person{

}
Person p1 = new Person();
Person p2 = new Man();
Man m1 = new Man();
System.out.println(p1 instanceof Man);//false
System.out.println(p2 instanceof Man);//true
System.out.println(m1 instanceof Man);//true

注意第一种情况, p1 instanceof Man ,Man 是 Person 的子类,Person 不是 Man 的子类,所以返回结果为 false。

String 类中的重写的 equals 方法

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = value.length;
        if (n == anotherString.value.length) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = 0;
            while (n-- != 0) {
                if (v1[i] != v2[i])
                    return false;
                i++;
            }
            return true;
        }
    }
    return false;
}

js的typeof和instanceof方法

jsonof是一个一元运算,放在一个运算数之前,运算数可以是任意类型。它会返回一个字符串,该字符串说明运算数的类型。

typeof 返回值有六种可能: “number”, “string”,“boolean” ,“object”, “function” 和 "undefined "

typeof(1);

typeof(NaN);

typeof(Number.MIN_VALUE);

typeof(Infinity);

typeof(“123”);

typeof(true);

typeof(window);

typeof(document);

typeof(null);

typeof(eval);

typeof(Date);

typeof(sss);

typeof(undefined);

在 JavaScript 中,判断一个变量的类型尝尝会用 typeof 运算符,在使用 typeof 运算符时采用引用类型存储值会出现一个问题,无论引用的是什么类型的对象,它都返回 “object”。ECMAScript 引入了另一个 Java 运算符 instanceof 来解决这个问题。instanceof 运算符与 typeof 运算符相似,用于识别正在处理的对象的类型。与 typeof 方法不同的是,instanceof 方法要求开发者明确地确认对象为某特定类型。例如:

// 判断对象是否为 “未定义” 值(即 undefined)。
coreUtil.isUndefined = function (obj) { return obj === undefined || typeof obj === “undefined”; };
var oStringObject = new String(“hello world”);
console.log(oStringObject instanceof String); // 输出 “true”
这段代码问的是“变量 oStringObject 是否为 String 对象的实例?”oStringObject 的确是 String 对象的实例,因此结果是"true"。尽管不像 typeof 方法那样灵活,但是在 typeof 方法返回 “object” 的情况下,instanceof 方法还是很有用的。

通常来讲,使用 instanceof 就是判断一个实例是否属于某种类型。例如:

. e x t e n d ( .extend( .fn.datagrid.methods, {
addEditor : function(jq, param) {
if (param instanceof Array) {
$.each(param, function(index, item) {
var e = $(jq).datagrid(‘getColumnOption’, item.field);
e.editor = item.editor;
});
} else {
//获取datagrid字段的属性
var e = $(jq).datagrid(‘getColumnOption’, param.field);
//给编辑器赋值
e.editor = param.editor;
}
},
removeEditor : function(jq, param) {
if (param instanceof Array) {
$.each(param, function(index, item) {
var e = $(jq).datagrid(‘getColumnOption’, item);
e.editor = {};
});
} else {
var e = $(jq).datagrid(‘getColumnOption’, param);
e.editor = {};
}
}
});
另外,更重的一点是 instanceof 可以在继承关系中用来判断一个实例是否属于它的父类型。例如:

// 判断 foo 是否是 Foo 类的实例 , 并且是否是其父类型的实例
function Aoo(){}
function Foo(){}
Foo.prototype = new Aoo();//JavaScript 原型继承

var foo = new Foo();
console.log(foo instanceof Foo)//true
console.log(foo instanceof Aoo)//true
上面的代码中是判断了一层继承关系中的父类,在多层继承关系中,instanceof 运算符同样适用。

console.log(Object instanceof Object);//true
console.log(Function instanceof Function);//true
console.log(Number instanceof Number);//false
console.log(String instanceof String);//false

console.log(Function instanceof Object);//true

console.log(Foo instanceof Function);//true
console.log(Foo instanceof Foo);//false
JavaScript instanceof 运算符代码

function instance_of(L, R) {//L 表示左表达式,R 表示右表达式
var O = R.prototype;// 取 R 的显示原型
L = L.proto;// 取 L 的隐式原型
while (true) {
if (L === null)
return false;
if (O === L)// 这里重点:当 O 严格等于 L 时,返回 true
return true;
L = L.proto;
}
}
Object instanceof Object,Function instanceof Function 和 Foo instanceof Foo 三个示例

Object instanceof Object

// 为了方便表述,首先区分左侧表达式和右侧表达式
ObjectL = Object, ObjectR = Object;
// 下面根据规范逐步推演
O = ObjectR.prototype = Object.prototype
L = ObjectL.proto = Function.prototype
// 第一次判断
O != L
// 循环查找 L 是否还有 proto
L = Function.prototype.proto = Object.prototype
// 第二次判断
O == L
// 返回 true
Function instanceof Function

// 为了方便表述,首先区分左侧表达式和右侧表达式
FunctionL = Function, FunctionR = Function;
// 下面根据规范逐步推演
O = FunctionR.prototype = Function.prototype
L = FunctionL.proto = Function.prototype
// 第一次判断
O == L
// 返回 true
Foo instanceof Foo

// 为了方便表述,首先区分左侧表达式和右侧表达式
FooL = Foo, FooR = Foo;
// 下面根据规范逐步推演
O = FooR.prototype = Foo.prototype
L = FooL.proto = Function.prototype
// 第一次判断
O != L
// 循环再次查找 L 是否还有 proto
L = Function.prototype.proto = Object.prototype
// 第二次判断
O != L
// 再次循环查找 L 是否还有 proto
L = Object.prototype.proto = null
// 第三次判断
L == null
// 返回 false


作者:wespten
来源:CSDN
原文:https://blog.csdn.net/qq_35029061/article/details/83988428
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/liuSC123/article/details/83989447