分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

关于js作用域问题

发布时间:2023-09-06 02:18责任编辑:郭大石关键词:js作用域

补充:

function Foo(name,age){ ???this.name=name; ???this.age=age; ???this.getName=function(){ ???????console.log(this); ???} ???????}obj = new Foo("文州",19)obj.getName() ???//这里把Foo看成类,大写是类,首字母小写是函数function test(){ ???console.log(this);}test()=== window.test()//函数执行相当于window.test().所以打印的this是window对象。(function(){ ???console.log(this)})()这个叫自执行函数。this指的还是window。题目0var name="女神"function Foo(name,age){ ???this.name=name; ???this.age=age; ???this.getName=function(){ ???????console.log(this); ???????????????(function(){ ???????????console.log(this.name) ??//自执行函数里面是window对象 ???????})() ???}}obj = new Foo("文州",19)obj.getName() 第一次打印文州,第二次打印女神想要都打印文州的话题目1var name="女神"function Foo(name,age){ ???this.name=name; ???this.age=age; ???this.getName=function(){ ???????console.log(this); ???????var that =this ???//把当前环境的this赋值给that.那么这样2个都打印文州 ???????(function(){ ???????????console.log(that.name) ??????????})() ???}}obj = new Foo("文州",19)obj.getName() //这样2个都执行文州。
题目2var name="女神"obj={ ???name:‘文州‘, ???age:19, ???getName:function(){ ???????console.log(this); ???????var that =this ???????????(function(){ ???????????console.log(that.name) ??????????})() ???}}obj.getName()//这个结果和上面的一样。只是(对象)的声明方式改变了而已。

关于js作用域问题

原文地址:https://www.cnblogs.com/geogre123/p/9792748.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved