最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

关于js函数中this的理解

来源:动视网 责编:小采 时间:2020-11-27 20:12:50
文档

关于js函数中this的理解

关于js函数中this的理解://关于this的问题 第一个情况: function Foo() { this.name=10; this.age=100; console.log(this) //{name:10, age:100} } // var p=new Foo(); new 一个对象,this指的就是即将new出来的对象 // Foo() 调用情况下,this指w
推荐度:
导读关于js函数中this的理解://关于this的问题 第一个情况: function Foo() { this.name=10; this.age=100; console.log(this) //{name:10, age:100} } // var p=new Foo(); new 一个对象,this指的就是即将new出来的对象 // Foo() 调用情况下,this指w


//关于this的问题 第一个情况:
 function Foo() {
 this.name=10;
 this.age=100;
 console.log(this) //{name:10, age:100}
 }
// var p=new Foo(); new 一个对象,this指的就是即将new出来的对象
// Foo() 调用情况下,this指window 
输出{window}
//关于this的问题 第二种情况:var obj={
 x:5,
 fn:function () {
 console.log(this); //obj{x:10,fn:function}
 console.log(this.x); //5
 }
};
obj();
 //函数作为对象的一个属性被调用时,this指该对象,即obj
//关于this的问题 第三种情况:var obj= {
 x: 10,
 fn: function () {
 console.log(this); //Window
 console.log(this.x); //undefined
 }
};
var fun=obj.fn;
 fun()
 //这里fn函数被赋值到另一个变量中,没有作为obj一个属性被调用,则this指window//关于this的问题 第四种情况:
 var obj={ x:20}
 var fn =function () {
 console.log(this); //Object {x:20}
 console.log(this.x) //20
 }
 fn.call(obj);
 //当一个函数被call或apply调用时,this则取传入的对象的值或者:var x=20;var fn = function(){	console.log(this) //window	console.log(this.x) //20};fn(); //调用fn函数时,this指的是window//关于this的问题 第五种情况:var obj={
 x:10,
 fn:function(){
 function f(){
 console.log(this); //window
 console.log(this.x) //undefined
 }
 f()
 }
};
obj.fn()
// 函数f虽然是在函数内部定义的,但仍然是普通函数,this指window.

文档

关于js函数中this的理解

关于js函数中this的理解://关于this的问题 第一个情况: function Foo() { this.name=10; this.age=100; console.log(this) //{name:10, age:100} } // var p=new Foo(); new 一个对象,this指的就是即将new出来的对象 // Foo() 调用情况下,this指w
推荐度:
标签: 方法 js 理解
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top