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. Swagger2使用参考

    GitHub例子: 参考博客: https://blog.csdn.net/sanyaoxu_2/article/details/80555328 http://www.cnblogs.com/Joi ...

  2. ECharts上手例子

    ECharts,一个使用 JavaScript 实现的开源可视化库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器 (IE8/9/10/11,Chrome,Firefox,Safari等 ...

  3. python基础知识点三

    内置函数和匿名函数 python 一共有68个内置的函数:它们就是python提供给你直接可以拿来使用的所有函数 内置函数的图:链接 :https://www.processon.com/mindma ...

  4. ActiveReports 大数据分析报告:贸易争端与中国企业数字化转型

    2018年11月12日至18日,亚太经合组织(APEC)领导人非正式会议首次在南太平洋最大岛国巴布亚新几内亚的首都莫尔兹比港举行,本次会议的主题是:“把握包容性机遇,拥抱数字化未来”. 面对全球不断变 ...

  5. Vue:(五)axios

    Axios是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中.axios主要是用于向后台发起请求的,还有在请求中做更多可控功能.官方不再维护vue-resource,推 ...

  6. ES6标准之箭头函数

    语法 具有一个参数的简单函数 var single = a => a single('hello, world') // 'hello, world' 没有参数的需要用在箭头前加上小括号 var ...

  7. JS中如何判断对象是对象还是数组

    JS中如何判断对象是对象还是数组 一.总结 一句话总结:typeof Array.isArray === "function",Array.isArray(value)和Objec ...

  8. RabbitMq(6) 如何保证消息不丢包

    RabbitMQ一般情况很少丢失,但是不能排除意外,为了保证我们自己系统高可用,我们必须作出更好完善措施,保证系统的稳定性. 下面来介绍下,如何保证消息的绝对不丢失的问题,下面分享的绝对干货,都是在知 ...

  9. 重写console.log的一些理解

    关于重写console.log的方式通常都是这样的: console.log = (function(oriLogFunc){ return function(str) { oriLogFunc.ca ...

  10. python学习(四)