JS 对象4连
1. 什么是 arguments 是啥?https://blog.csdn.net/qq_16339527/article/details/53231725
2.call 和 apply的区别
https://blog.csdn.net/ganyingxie123456/article/details/70855586
3.深入理解JS构造函数
https://www.cnblogs.com/wangyingblog/p/5583825.html
4.深入理解JS面向对象
http://www.cnblogs.com/tugenhua0707/p/5068449.htm
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>call的测试</title>
</head>
<body>
<div>
这是call 的测试
1.每个函数都包含两个非继承而来的方法:call() 和 apply()方法
2.相同点:两个方法的作用是一样的
<hr />
都是在特定的作用域中使用函数,等于设置函数体内this 对象的值,以扩充函数赖以运行的作用域。
<hr />
<p id="dk"></p>
<input type="text" id="int"/>
</div>
<script>
var dk= document. getElementById('dk');
var int = document.getElementById('int')
window.color='red document.color='yellow
var s1 = {color:'blue'};
function changeColor(){
console.log(this.color);
}
changeColor.call(); //输出结果red 默认传递参数
changeColor.call(window); //输出 red
changeColor.call(document); //输出 yellow
changeColor.call(s1); //输出 blue
changeColor.call(this); //输出 red
var pet = {
words:'--d-d-d-',
speak:function (say)
{
dk.innerText = say +' ' +this.words;
}
}
var lmm={
words:'额这是lmm的word'
}
function mp(name)
{
this.name = name;
this.sp=function(){
// console.log(this.words)
dk.innerText = this.name
}
}
function dh()
{
mp.apply(this, arguments);
}
var dog = new dh('小王')
// enter 事件
int.onkeydown=function(event){
var e = event || window.event;
if(e && e.keyCode==13){ // enter 键
// var s1 = int.value;
// // pet.speak(s1)
// // 这里将this的指向改成了lmm
// pet.speak.call(lmm,'我擦厉害了')
dog.sp();
}
};
function add() {
var len = arguments.length,
sum = 0;
for(;len--;){
sum += arguments;
}
return sum;
}
console.log( add(1,2,3) ); //6
console.log( add(1,3) ); //4
console.log( add(1,2,3,5,6,2,7) ); //26
</script>
</body>
</html>
页:
[1]