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/ .学习网站上有对应 ...
随机推荐
- <PHP>字符串处理代码
字符串处理: strlen("aaa");取字符串的长度 *** strcmp("aaa","aaa");比较两个字符串 ...
- AngularJs学习笔记4——四大特性之双向数据绑定
双向数据绑定 方向1:模型数据(model)绑定到视图(view) 实现方法:①.{{model变量名}} ②.常用指令(ng-repeat) 方向2:将视图(view)中用户输入的数据绑定到模型数 ...
- git config配置文件 (共有三个配置文件)
设置 git status的颜色. git config --global color.status auto 一.Git已经在你的系统中了,你会做一些事情来客户化你的Git环境.你只需要做这些设置一 ...
- python - 面向对象(二)
类的三大特性 类的三大特性包括: 封装.继承.多态 一.封装 封装就是将类所用到的所有字段.属性.方法都包含在类代码段里面,当实例调用直接调用类中的方法即可. class People(object) ...
- 转载:android——eclipse如何去除Ctrl+shift+R组合键查找到的.class文件
转载自:http://blog.csdn.net/virgilli/article/details/22500409 AS里面的build文件下一堆的.class 文件,当你要定位资源文件的时候,有些 ...
- Js弹性漂浮广告代码
<html><head><meta http-equiv="Content-Type" content="text/html; charse ...
- Android与JS混编(js调用java)
项目中需要使用android与js的混编来开发app. 下面就介绍一下吧. 有时候我们需要用js调用native控件,要想实现这个功能,我们需要做的就只有三步: 1.允许webview执行js脚本 2 ...
- jwplayer修改logo右键版权
jwplayer二次编译,可以自定义自己的logo和右键版权.
- 图文混排——用表情代替"[文字]"
1.简单设置带属性的字符串 定义一个NSMutableAttributedString带属性的字符串 NSMutableAttributedString *str = [[NSMutableAttri ...
- IntelliJ IDEA启动web项目时突然变慢的原因
在使用IntelliJ IDEA开发web项目过程中,有两次项目启动非常慢,大约要200s的时间: 第一次忘记是怎么解决的,第二次出现后,我就直接重新下载了代码,然后部署,启动,时间有恢复正常,只用了 ...