JS self=this
2024-10-08 02:08:45
1.每个函数都会有自己的this和arguments;this对象绑定运行环境,arguments绑定调用参数。
2.全局函数:this和全局环境绑定,浏览器指向全局window对象(node.js中指向全局global对象)。
3.类成员函数:this和实例对象绑定,指向类实例对象。
4.类成员函数中的局部函数:this和全局环境绑定,指向全局window对象(node.js中指向全局global对象)。也就是在这种情况下,如果需要使用到类实例对象的引用,就需要将类实例对象的引用this记录下来,提供给该局部函数使用。(个人理解为闭包的作用。)
下面例子在node.js上运行,在浏览器上运行请将global替换为window。
5.全局函数的例子:
var g_func = function(){
console.log(this == global);
} g_func();
输出
true
6.类成员函数例子
var g_object = function(){
this.func = function(){
console.log("func this == global : ",this == global);
} this.no_self_func = function(){
var inner_func = function(){
console.log("no_self_func this == global : ",this == global);
} inner_func();
} this.self_func = function(){
var self = this;
var inner_func = function(){
console.log("self_func this == global : ",this == global);
console.log("self_func self == global : ",self == global);
} inner_func();
}
} var obj = new g_object();
obj.func();
obj.no_self_func();
obj.self_func();
输出
func this == global : false
no_self_func this == global : true
self_func this == global : true
self_func self == global : false
PS:这种特性和lua很像。
以上。
最新文章
- AI (Adobe Illustrator)详细用法(四)
- 打包ane之后在FB上生成ipa的阶段错误
- Power Builder的学习
- Unity的物理引擎是如何实现碰撞的呢?
- 使用DialogFragment创建对话框总结
- Java笔记(十四)……抽象类与接口
- 在TMemo上画一条线
- 重装eclipse要做的事
- C# 与 VB.NET 对比
- thinkphp模版调用函数方法
- TCP的定时器系列 — 零窗口探测定时器
- 在网页中使用particlesjs实现背景的动态粒子特效
- BZOJ3105-新Nim游戏
- laravel中判断当前页面与连接地址是否一致,并添加效果:
- IIS:IIS 8.5下设置404错误页
- 哈希与位图(Hash and BitMap)
- 第二次作业:找Bug
- hdu1517找规律
- CodeForces 558C Amr and Chemistry (位运算,数论,规律,枚举)
- Java-Maven-Runoob:Maven 构建配置文件
热门文章
- 腾讯云 网站开启HTTPS
- python之udp协议的套接字
- 第五章 CSS常用属性笔记
- tls 双向认证 client端代码例子
- python --- 03 整型 bool 字符串 for循环
- topcoder srm 702 div1 -3
- 【无法使用yum安装软件】使用yum命令安装软件提示No package numactl.x86_64 available.
- hihoCoder week14 无间道之并查集
- How can I move a MySQL database from one server to another?
- Vue.directive自定义指令