发表于: 2017-01-18 20:47:17
3 1179
今天完成的事情:
修改了下拉菜单,选择并原生实现搜索功能,但是组合搜索逻辑上没理出来。
修改了ppt,并实际把继承各种方法的实现了一遍。
this的用法,什么场景用指向全局对象?什么时候用指向局部。
网络上常用的解释call的代码如下:
function
Animal(){
this
.name =
"Animal"
;
this
.showName =
function
(){
alert(
this
.name);
}
}
/**定义一个Cat类*/
function
Cat(){
this
.name =
"Cat"
;
}
/**创建两个类对象*/
var
animal =
new
Animal();
var
cat =
new
Cat();
//通过call或apply方法,将原本属于Animal对象的showName()方法交给当前对象cat来使用了。
//输入结果为"Cat"
animal.showName.call(cat,
","
);
//animal.showName.apply(cat,[]);
这样的代码和我以前一直的理解也都是一样的。我以前一直这样理解call或者apply的方法
mal.showName.call(cat,","); ani
其实也就是将showN
ame里面的this本来是指向animal,call调用后this指向cat。 这样理解,也就能解释了。
但是今天搜索资料时候看到别人另外一段demo:
var
test = 111;
function
_add()
{
// this.test = 444; //注释①
alert(
this
.test);
}
function
_sub()
{
this
.test = 222;
alert(
this
.test);
}
_add();
_sub();
_add.call(_sub);
//未去掉注释①:undefined 去掉:444
var
d =
new
_add();
//结果等同于_add.call(_sub);
1. _add.call(_sub) _add.call(_sub) = window._add.call(_sub),
this开始是指向window的,call掉用后为什么就又指向哪里去了呢?
2. _add.call(new _sub())如果这样写得话呢,那很明显结果this是指向 _sub的对象的,
但是,我们又常说JS里面的方法(function)也是对象,
那么是不是说 _add.call(_sub) 等于如下代码:
var
_sub =
new
function
(){
this
.test = 222;
alert(
this
.test);
}
_add.call(_sub)
//222
3.那为什么以上1和2的返回值是不一样的呢?一个是undefined,一个是222?
明天计划的事:
继续任务7的学习
遇到的困难:
flter怎么实现组合搜索的过滤
收获:
制作ppt的技巧,深入学习
分享小课堂:
今天给大家讲了一下继承的用法,平时代码里很少直接的想到继承的用法。
组合继承避免了原型链和借用构造函数的缺点,融合了他们的优点。熟记这种用法来的继承实现。书到用时方恨读书少
评论