JavaScript思维导图——Day 10(this,克隆方法)

在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
	<script type="text/javascript">
		// var arr = [];
		// console.log(typeof(arr));
		// isNaN("100");
		// function myIsNaN(num) {
		// 	var ret = Number(num);
		// 	ret += "";
		// 	if (ret == "NaN") {
		// 		return true;
		// 	}else{
		// 		return false;
		// 	}
		// }
		// console.log(myIsNaN("100"));
		// console.log(isNaN("100"));

		//this ---> window

		// function test(c) {

		// 	//var this = Object.create(test.prototype)
		// 	//{__proto__ : test.prototype}
		// 	var a = 123;
		// 	function b() {
		// 	}
		// }
		// test(1);

		// AO {
		// 	arguments : [1],
		// 	this :window,
		// 	c : 1,
		// 	a = undefined,
		// 	b = function () {}
		// }

		// new test();
		// function test() {
		// 	console.log(this); //windws
		// }
		// test();

		//全局this 指向 windows

		// console.log(this);


		// var obj = {
		// 	a : function () {
		// 		console.log(this.name)
		// 	},
		// 	name : 'abc'
		// }
		// //谁调用这个方法这个里面的this 就指向谁

		// obj.a();

		// var name = "222";
		// var a = {
		// 	name : "111",
		// 	say  : function () {
		// 		console.log(this.name);
		// 	}
		// }

		// var fun = a.say;
		// fun(); //222
		// a.say();//111
		// var b = {
		// 	name : "333",
		// 	say : function (fun) {
		// 		fun();
		// 		//this ---> b
		// 		//say  : function () {
		// 		//console.log(this.name);
		// 		//window.this
		// 		//没人调用就是window 的调用
		// 	}
		// }
		// b.say(a.say);//222
		// b.say = a.say;
		// b.say();//333

		// function test() {
		// 	console.log(arguments.callee);
		// }
		// test();

		// var num = (function (n) {
		// 	if (n == 1) {
		// 		return 1;
		// 	}
		// 	return n * arguments.callee(n - 1);
		// }(20));

		// console.log(num);

		// function test() {
		// 	console.log(arguments.callee);
		// 	function demo() {
		// 		console.log(arguments.callee);
		// 	}
		// 	demo();
		// }
		// test();

		//克隆方法 浅层克隆
		var obj = {
			name : 'abc',
			age : 123,
			sex : 'male',
			wife : {
				name : "bcd",
				son : {
					name : "aaa"
				}
			}
		}

		var obj1 = {};

		function clone(origin , target) {
			var target = target || {};
			for(var prop in origin){
				target[prop] = origin[prop];
			}
			return target;
		}
		// clone(obj,obj1);

		function deepClone(origin, target) {
			var target = target || {},
			    toStr = Object.prototype.toString,
			    arrStr = "[object Array]";

			    for(var prop in origin){
			    	if (origin.hasOwnProperty(prop)) {

			    		if (origin[prop] != "null" && typeof(origin[prop]) == 'object') {
			    			// if (toStr.call(origin[prop]) == arrStr) {
			    			// 	target[prop] = [];

			    			// }else{
			    			// 	target[prop] = {};
			    			// }
			    			target[prop] = toStr.call(origin[prop]) == arrStr ? [] : {};

			    			deepClone(origin[prop] , target[prop]);


			    		}else{
			    			target[prop] = origin[prop];
			    		}
			    	}
			    }
			    return target;
		}
		deepClone(obj,obj1);





	</script>

</body>
</html>
发布了82 篇原创文章 · 获赞 21 · 访问量 2027

猜你喜欢

转载自blog.csdn.net/weixin_45174208/article/details/104288748