<h2>this</h2>
1.普通函數(shù)
function t(){
this.age=23;
console.log(this);
}
t()//調(diào)用者為null,this為null時(shí)js將this指向了window察净,在es5之后會(huì)拋出異常
2.作為對(duì)象的方法來調(diào)用
var intro=function(){return "mynameis"+this:name}
var dog={name:"123",intro:intro}
dog.intro();
var cat={name:"789"}
cat.intro=dog.intro
cat.intro();//789
3.構(gòu)造函數(shù)
function cat(name){
this.name=name;
this.color=color;
}
//方法new的瞬間,得到一個(gè)空對(duì)象慎璧,運(yùn)行this指向空對(duì)象葡盗,運(yùn)行方法{}.name=name;
var cat=new cat()//得到一個(gè)新對(duì)象 從而觸發(fā)了一個(gè)函數(shù)
4.通過call和apply可以改變this的指向
函數(shù)名.call($,$,$);
1.將函數(shù)的this指向?qū)ο?2.運(yùn)行函數(shù)佳头,傳參數(shù)$,$,$;
函數(shù)名.apply(obj, array[]);
5.閉包
function t(){
var age=23;
return function(){
console.log(age++);
}
}
var tmp=t();
var age=1000;
tmp();//
6.構(gòu)造方法
function Dog(name,color){
this.name=name;
this.color=color;
}
var dog=new Dog("donggua","shaoyan");
console.log(dog);
7.私有屬性
function girl(name,lover){
var nm=name;
var love=lover;
this.getlove=function(){
return love;
}
this.getname=function(){
return nm;
}
}
var girl=new girl("123","234");
console.log(girl);
console.log(girl.getlove());
8.原型繼承
對(duì)著貓來畫老虎
function cat(){
this.climb=function(){
alert("ps")
}
}
function Tiger(){
this.hunt=function(){
alert("dl");
}
}
Tiger.prototype=new cat();
var tiger=new Tiger();
tiger.hunt();
tiger.climb()
//console.log(tiger)
Tiger.prototype.song=function(){
alert("789")
}
tiger.song();
9.原型冒充
function good(){
this.iq=120
this.study=function(){
console.log("gao")
}
}
function bad(){
good.call(this);
this.play=function(){
console.log("mhxy")
}
}
var good=new good();
var bad=new bad();
good.study();
bad.play();
bad.good();
10.原型復(fù)制
function good(){
this.iq=120
this.study=function(){
console.log("gao")
}
}
function bad(){
this.play=function(){
console.log("mhxy")
}
this.extend=function(obj){
for(var k in obj){
this[k]=obj[k];
}
}
}
var good=new good();
var bad=new bad();
bad.extend(good);
console.log(bad);
11.函數(shù)也是一種對(duì)象
$("#id")
$.ajax