JS中简单的this学习
var x = 10;
var foo = {
x: 20,
bar: function() {
alert(this.x);
}
}
var bar = foo.bar;
foo.bar(); //20
bar(); //10
function bar() {
console.log(this);
}
bar(); //window 其实是global对象
console.log(bar === bar.prototype.constructor); //true
bar.prototype.constructor(); //bar.prototype
The value of this in a function context is provided by the caller and determined by the current form of a call expression (how the function call is written syntactically). If on the left hand side from the call parentheses ( ... ), there is a value of Reference type then this value is set to the base object of this value of Reference type. In all other cases (i.e. with any other value type which is distinct from the Reference type), thisvalue is always set to null. But since there is no any sense in null for this value, it is implicitlyconverted to global object.
var foo = {
bar: function () {
console.log(this);
}
};
foo.bar(); // Reference, OK => foo
(foo.bar)(); // Reference, OK => foo
(foo.bar = foo.bar)(); // global
(false || foo.bar)(); // global
(foo.bar, foo.bar)(); // global
function person() {
console.log(this);
}
person(); //window(global)
JS中简单的this学习的更多相关文章
- [转] JS中简单的继承与多态
这里讲了一个最最最简单的JS中基于原型链的继承和多态. 先看一下以下这段代码的实现(A是“父类”,B是“子类”): var A = function(){ this.value = 'a'; this ...
- 在JS中简单实现Formatter函数
JS原生并没有提供方便使用的Formatter函数,用字符拼接的方式看起来混乱难读,而且使用起来很不方便.个人感觉C#里提供的语法比较好用,如: String.Format("Welcome ...
- JS中的bind方法学习
EcmaScript5给Function扩展了一个方法:bind 众所周知 在jQuery和prototype.js之类的框架里都有个bind jQuery里的用途是给元素绑定事件 $("# ...
- 1.1 js中函数定义解析(学习笔记)
1.1.1函数的分类 函数声明式 :使用function声明函数,并指定函数名. 函数表达式:使用function声明函数,但未指定函数名. 函数表达式2.匿名函数,匿名函数有很多作用,赋予一个变量则 ...
- JS中简单的二级城市联动
代码奉上: <!DOCTYPE html><html><head> <meta charset="UTF-8"> < ...
- JS中简单原型的使用
- js中简单操作
去空格:ss.replace(/\s/g,""); 数组: 千万不能用in操作符 in 是说存不存在这个key而不是value! var a = [66,99,77]; 66 in ...
- 深入探究js中无所不在的this
黄金守则: this对象是在运行时基于函数的执行环境绑定的:在全局函数中,this等于window而当函数被作为某个对象的方法调用时, this等于那个对象. 下面是一些相关实践: --------- ...
- Node.js中的Session,不要觉得简单哦。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博客地址为http://www.cnblogs.com/jasonnode/ .学习网站上有对应 ...
随机推荐
- SelectSort 选择排序
//SelectSort (( O(n²))) public class TestSelectSort { public int[] selectSortArray(int[] arr){ int m ...
- Spring 小示例
通过一个简单的示例来初步理解Spring框架 1.创建java工程,导入相应Spring包,放在lib文件夹中 2.接口 IHelloMessage package com.jike.spring. ...
- qt tablewidget中单个和批量删除代码如下(部分)截图如下
def coltable(self):#行删除 row=self.downwidget.currentRow() select=self.downwidget.isItemSelected ...
- class 类(4)
要将类实例化,然后通过实例来调用类的方法(函数).在此,把前面经常做的这类事情概括一下: 方法是类内部定义函数,只不过这个函数的第一个参数是self.(可以认为方法是类属性,但不是实例属性) 必须将类 ...
- Java基础 -- 冒泡排序算法(带详细注释)
冒泡排序的要点: 1.多轮排序,每轮排序中选出最大的元素放在最顶端,并且下次排序不再使用该元素; 2. 使用双for循环,外层for循环控制要排序的次数(轮数), 内层for循环控制当前要排序的元素并 ...
- Android 按二次后退键退出应用程序
前言 欢迎大家我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...
- Eclipse+Java+OpenCV246人脸识别
1.环境搭建:见上一篇博客 整个项目的结构图: 2.编写DetectFaceDemo.java,代码如下: package com.njupt.zhb.test; import org.opencv. ...
- TTB 基本
中文名 ,线程构建模块 外文名 Thread Building Blocks 缩 写 TBB 开 发 intel 目录 1线程构建模块 2黑体亮温 3斜交载重轮胎 4串联球轴承 1 ...
- android studio 开发android app 真机调试
大家都知道开发android app 的时候可以有2种调试方式, 一种是Android Virtual Device(虚拟模拟器) ,另一种就是真机调试. 这里要说的是真机调试的一些安装步骤: 1. ...
- razor类型强制转换
一.如果后台得到的是一个List类型 1.后台得到数组数据 var dutyList = IOCFactory.R<IEmployeeContract>().BatchFind(m =&g ...