javaScript中instanceof和typeof知识点

直接上代码和截图

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>javaScript中instanceof和typeof知识点</title>
<style type="text/css">
input[type=button] {
	background-color: #8E388E;
	border: 0px solid #8E388E;
	color: #fff;
	width: 160px;
	height: 40px;
	border-radius: 6px; /*把边框做成圆角*/
}

</style>
<script type="text/javascript">
//window对象有name属性
console.log("*****", name, "#####");
console.log("*****", name, window.name, window, "#####");

function fn() {
	//
	console.log("***1***", arguments.callee);
	//***2*** function function
	console.log("***2***", typeof arguments.callee, typeof(arguments.callee));
	//***3*** true
	console.log("***3***", arguments.callee instanceof Function);
}

fn();

console.log("*************");
var roles = new Array("建宁公主", "沐剑屏", "双儿", "曾柔");
//object object true true
console.log(typeof roles, typeof(roles), roles instanceof Object, roles instanceof Array);
var dog = {name : "旺财", age : 3, hobby : "啃骨头"};
//object object true false
console.log(typeof dog, typeof(dog), dog instanceof Object, dog instanceof Array);
console.log("*************");

function Aoo(){}
function Foo(){}
Foo.prototype = new Aoo();//javaScript原型继承
var foo = new Foo();
//instanceof表示的就是原型链的结构
console.log("***666***", foo instanceof Foo)//***666*** true
console.log("**888***", foo instanceof Aoo)//**888*** true

var cat = {name : "HelloKitty", age : 2};
var c2 = cat;
//***A*** object object true
console.log("***A***", typeof(cat), typeof cat, cat instanceof Object);
//***B*** object object true
console.log("***B***", typeof(c2), typeof c2, c2 instanceof Object);
//***C*** true true
console.log("***C***", c2 == cat, c2 === cat);


console.log("***D***", Object instanceof Function) //***D*** true

console.log("***E***", Function instanceof Object) //***E*** true

console.log("***F***", Function instanceof Function) //***F*** true

var a = "江西省赣州市于都县";
var b = 66;
var c = 88.99;
var d = true;
var h = null;
var k;
var x = {};
var y = [];
var z = new Array("黄晓明", "周杰伦", "范冰冰");
var p = function() {console.log("hello everyone")};
//***80*** false false
console.log("***80***", a instanceof String, b instanceof Number);
//***81*** false false
console.log("***81***", c instanceof Number, d instanceof Boolean);
//***82*** false false
console.log("***82***", h instanceof Object, k instanceof Object);
//***83*** true true true
console.log("***83***", x instanceof Object, y instanceof Object, y instanceof Array);
//***84*** true true true true
console.log("***84***", z instanceof Object, z instanceof Array, p instanceof Function, p instanceof Object);

//instanceof操作符用来判断引用类型

//***85*** false false
console.log("***85***", a instanceof Object, b instanceof Object);
//***86*** false false
console.log("***86***", c instanceof Object, d instanceof Object);
//***87*** false false
console.log("***87***", null instanceof Object, undefined instanceof Object);
//***88*** string number
console.log("***88***", typeof a, typeof b);
//***89*** number boolean
console.log("***89***", typeof c, typeof d);
//***90*** object undefined object undefined
console.log("***90***", typeof h, typeof k, typeof null, typeof undefined);
//***91*** object object object function
console.log("***91***", typeof x, typeof y, typeof z, typeof p);
//***92*** true false
console.log("***92***", null == undefined, null === undefined);
//***93*** false false
console.log("***93***", null == Object, null === Object);
//***94*** object false boolean boolean
console.log("***94***", typeof null, typeof null == Object, typeof (null == Object), typeof (null === Object));
//***95*** object true true
console.log("***95***", typeof null, typeof null == "object", typeof null === "object");
//***96*** object boolean boolean
console.log("***96***", typeof null, typeof (null == "object"), typeof (null === "object"));
//***97*** object true true
console.log("***97***", typeof null, (typeof null) == "object", (typeof null) === "object");

console.log("*************");

function fn1() {
	var testStr = "[object Object]";
	//slice(startIndex, endIndex)注意:不包括endIndex
	//-1表示最后一个字符的下标位置,-2表示倒数第二个字符的下标位置,-3,-4等等,以此类推
	console.log(testStr, testStr.slice(8, -1));
	//赣州市于都县渡江大
	console.log(testStr, "江西省赣州市于都县渡江大道".slice(3, -1));
}
</script>
</head>
<body style="background-color: #CCE8CF;">
<h1>javaScript中instanceof和typeof知识点</h1>
<input type="button" value="字符串的slice()函数" onclick="fn1();">
<div id="div1" style="background-color: Wheat; height: 100%;">
</div>
</body>
</html>

最近比较懒,这样不好不好!

猜你喜欢

转载自blog.csdn.net/czh500/article/details/115258808