this指向问题 --无return
this的指向在函数定义的时候是确定不了的只有在函数执行的时候才能确定this到底指向谁。this指向上一级对象
1.函数调用,this指向window
var color = "red"
function test() {
var color = "yellow"
console.log(this.color) //red
console.log(this) //window
}
test()
var a = 8;
var c = {
a :10,
b:{
a: 12,
fn: function() {
a: 13
console.log(this.a) //
console.log(this) //window
}
}
}
var test = c.b.fn
14 test()
2.构造函数,this指向实例对象
function user(name, age) {
this.name = name
this.age = age
this.say = function() {
console.log(this.name) //wu
console.log(this) //user{name: 'wu'...}
}
}
var wus = new user('wu', 12)
wus.say()
function Fn() {
this.name = 'wu'
console.log(this) //Fn
}
var jia = new Fn()
console.log(jia.name) //wu
3.apply,call上下文调用, this指向传入的第一个参数(改变this指向)
var a = {
name:"wu",
fn:function(){
console.log(this.name); //wu
}
}
var b = a.fn;
b.call(a);
var a = {
name:"wu",
fn:function(){
console.log(this); // window
}
}
var b = a.fn;
b.call(null);
var a = {
name:"wu",
fn:function(b,c){
console.log(this.name); //wu
console.log(b+c); //12qw
}
}
var d = a.fn;
d.apply(a,[12,"qw"]);
4.方法调用,this指向调用对象
var a = 8;
var c = {
a :10,
b:{
a: 12,
fn: function() {
a: 13
console.log(this.a) //
console.log(this) //b
}
}
}
var test = c.b.fn()
var color = "red"
var test= {
color :"yellow",
getColor: function() {
console.log(this.color) //yellow
console.log(this) //test
}
}
test.getColor() // === window.test.getColor()
getColor() // is not defined
this指向问题 --无return的更多相关文章
- Python3基础 函数 无return、return 空或None 的效果相同
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- LeetCode(3):无重复字符的最长子串
Medium! 题目描述: 给定一个字符串,找出不含有重复字符的 最长子串 的长度. 示例: 给定 "abcabcbb" ,没有重复字符的最长子串是 "abc" ...
- folly无锁队列,尝试添加新的函数
1. folly是facebook开源的关于无锁队列的库,实现过程很精妙.folly向队列中添加节点过程,符合标准库中的队列的设计,而取出节点的过程,则会造成多个线程的分配不均.我曾经试着提供一次 取 ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
- 函数:this & return、break、continue、exit()
this this:的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象在调用的时候才能决定,谁调用的就指向谁. 情景1:指向 ...
- 函数 return
return 的作用 一.返回一个值给函数,主函数调用这个函数后能得到这个返回的值.二.结束函数,例如你运行到一个地方,虽然后面还有代码但是你不想再继续运行,这时就可以直接用 return:这条语句来 ...
- 当try-catch-finally代码块遇上return,代码执行流程是怎样
这里打算用一个Java读取文件内容的例子来测试,文件存在,不抛异常,文件不存在,则抛出FileNotFoundException: Java读取文件代码如下: /** * 根据路径和文件名获取内容 * ...
随机推荐
- BinaryTree
public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode ...
- js运行机制详解:event loop
总结 阮一峰老师的博客 一.为什么JavaScript是单线程 JavaScript语言的一大特点就是单线程 那么,为什么JavaScript不能有多个线程呢?这样能提高效率啊. JavaScript ...
- JsonWebToken Demo(转)
namespace TokenTest { using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System. ...
- angular-material(一)
1.引入文件(angular-material.css.angular.min.js.angular-animate.js.angular-aria.min.js.angular-material.j ...
- centos7 安装xinetd,telnet
安装方式:yum [root@master ~]# yum list |grep telnettelnet-server.x86_64 1:0.17-59.el7 ...
- CentOS下redis-cli安装
Step1:安装gcc wget等系统lib yum install -y gcc wget Step2:获取redis稳定版并解压 cd /tmp wget http://download.redi ...
- MVC 深入讲解Routing _路由规则【八】
一.客户端=>控制器 在项目中我们引用了system.web.routing, 如果第一个匹配成功了,那么后面的都不会再匹配. 1. routing的作用: 确定colltroller,确定ac ...
- [Java] int 转换为BigDecimal
new BigDecimal(int i); BigDecimal.parseBigDecimal(String.valueOf(int i));
- linux安装redis ,mariadb
linux下安装软件方法 1 rpm (不推荐使用) 2 yum 安装(非常方便快捷) 3 编译安装(需要自定制的时候才使用) 安装mariadb(mysql) 1 使用官方源安装mariadb vi ...
- Sumo生成数据
1.生成input_net.net.xml文件 参数解释:http://www.sumo.dlr.de/userdoc/NETGENERATE.html#Grid_Network 1).生成grid ...