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. NSIS控制面板中显示安装包的大小和禁止多个安装程序实例

    转载:http://www.yhxs3344.net/jscript/nsis 转载:http://www.yhxs3344.net/archives/1292 1.控制面板中显示安装包的大小 ;需要 ...

  2. Oracle使用——oracle复制表

    复制表结构和数据 create table table_name_new as select * from table_name_old; 复制表结构 ; 复制表数据(全量插入数据) 两个表结构相同 ...

  3. 牛客练习赛26—D xor序列 —线性基

    这是我第一次写关于线性基的题目.其实这题很好理解,先把给出的数能异或出的值给存在p数组里面,p[i]代表着该异或出的数的最高位为第i位且为1. 求出来后,再把x,y处理下,然后直接一位一位的判断是否为 ...

  4. 将php-fpm添加至service服务

    1. 使用命令:cd /usr/local/php/etc,进入etc目录,编辑 php-fpm.conf 文件,将 ;pid = run/php-fpm.pid  前面的分号去掉 2. 重启php- ...

  5. EmailHelper

    注:个人邮箱发送时需要将邮箱密码设置为邮件授权码 邮件发送帮助类一: public class EmailHelper { /// <summary> /// 发送邮件 /// </ ...

  6. ceph储存的S3接口实现(支持断点续传)

    最近公司准备接ceph储存,研究了一番,准备用亚马逊的s3接口实现,实现类如下: /** * Title: S3Manager * Description: Ceph储存的s3接口实现,参考文档: * ...

  7. Practical Node.js (2018版) 14章, async code in Node

    Asynchronous Code in Node 历史上,Node开发者只能用回调和事件emitters. 现在可以使用一些异步的语法: async module Promises Async/aw ...

  8. The `android.dexOptions.incremental` property is deprecated and it has no effect on the build process.

    编译报错:The android.dexOptions.incremental property is deprecated and it has no effect on the build pro ...

  9. android -------- MVP+DataBinding 的使用

    今天来说说MVP+DataBinding 的使用 以一个登录案例来讲解 布局:(ConstraintLayout 作为根布局) <layout> <data> <vari ...

  10. spring cloud(一)带你进入分布式

    spring cloud是近年来比较火的热门话题,很多大型公司也渐渐转型使用spring cloud来完善各种开发模式,我认为主要是由spring团队开发由来,致使会有那么多的使用者,在java的领域 ...