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的更多相关文章

  1. Python3基础 函数 无return、return 空或None 的效果相同

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  2. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  3. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  4. LeetCode(3):无重复字符的最长子串

    Medium! 题目描述: 给定一个字符串,找出不含有重复字符的 最长子串 的长度. 示例: 给定 "abcabcbb" ,没有重复字符的最长子串是 "abc" ...

  5. folly无锁队列,尝试添加新的函数

    1. folly是facebook开源的关于无锁队列的库,实现过程很精妙.folly向队列中添加节点过程,符合标准库中的队列的设计,而取出节点的过程,则会造成多个线程的分配不均.我曾经试着提供一次 取 ...

  6. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现

    最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...

  7. 函数:this & return、break、continue、exit()

    this this:的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象在调用的时候才能决定,谁调用的就指向谁. 情景1:指向 ...

  8. 函数 return

    return 的作用 一.返回一个值给函数,主函数调用这个函数后能得到这个返回的值.二.结束函数,例如你运行到一个地方,虽然后面还有代码但是你不想再继续运行,这时就可以直接用 return:这条语句来 ...

  9. 当try-catch-finally代码块遇上return,代码执行流程是怎样

    这里打算用一个Java读取文件内容的例子来测试,文件存在,不抛异常,文件不存在,则抛出FileNotFoundException: Java读取文件代码如下: /** * 根据路径和文件名获取内容 * ...

随机推荐

  1. mvc 之 学习地址

    https://blog.csdn.net/mss359681091/article/details/52135861

  2. HAOI 2018 染色(容斥+NTT)

    题意 https://loj.ac/problem/2527 思路 设 \(f(k)\) 为强制选择 \(k\) 个颜色出现 \(s\) 种,其余任取的方案数. 则有 \[ f(k)={m\choos ...

  3. el-checkbox遇到的问题

    在官网中有实例 <template> <el-checkbox :indeterminate="isIndeterminate" v-model="ch ...

  4. css js 兼容问题

    js  兼容问题 1. document.form.item 问题问题:代码中存在 document.formName.item("itemName") 这样的语句,不能在FF下运 ...

  5. Spark面试相关

    Spark Core面试篇01 随着Spark技术在企业中应用越来越广泛,Spark成为大数据开发必须掌握的技能.前期分享了很多关于Spark的学习视频和文章,为了进一步巩固和掌握Spark,在原有s ...

  6. ehcache 简介和基本api使用

    文章转载自: https://blog.csdn.net/zhouzhiwengang/article/details/59838105 1.ehcahce简介 在开发高并发量,高性能的网站应用系统时 ...

  7. Appium Desktop-Permission to start activity denied.

    可能情况1:activity查找错误 如何查找activity (1)确保手机和电脑已连接   adb devices (2)确保在你手机上,要测试的包启动着 (3)dos运行:adb shell d ...

  8. postman(三):添加断言

    进行接口测试时,添加断言时必不可少的,断言就是判断响应内容与预期返回是否一致 进行接口测试时,添加断言时必不可少的,断言就是判断响应内容与预期返回是否一致   postman可以在请求模块的Tests ...

  9. python+unittest+requests+HTMLRunner编写接口自动化测试集

    问题描述:搭建接口测试框架,执行用例请求多个不同请求方式的接口 实现步骤: ① 创建配置文件config.ini,写入部分公用参数,如接口的基本url.测试报告文件路径.测试数据文件路径等配置项 [D ...

  10. SpringBoot之依赖注入DI

    相关注解: @Component @Service @Controller @Repository --------------------------------------------- @Inj ...