美文网首页
Js中的this

Js中的this

作者: jasmine_jing | 来源:发表于2017-04-01 11:00 被阅读0次

this是Javascript语言的一个关键字它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用;
以下分为多重情况 说明this的在不同的作用域里 指向也是不同的

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
</head>

<body>
<script>
//一、全局的this在浏览器下 等于 window
/*
console.log(this.document === document);//true
console.log(this === window);//true
this.a = 31;
console.log(window.a);//31


//二、一般函数的this(浏览器)
function f1(){
    return this;
    }
    
    f1() === window;  //true,global object

//严格模式下
function f2(){
    "use strict";
    return this;
    }
    f2() ===undefined;//true
    
    
//三、作为对象方法的函数的this   o.f();
var o={//对象字面量o
    prop :21,//prop属性
    f:function(){//函数作为对象的属性 叫做对象.方法
        return this.prop;
        }
    };
    console.log(o.f());//21

var p={pro:22};
function independent(){//直接调用independent this指向浏览器window对象
    return this.pro; 
    }
    p.f=independent;
    console.log(p.f());
//四、原型链上的this   
var  o={f:function(){return this.a +this.b;}};
var p = Object.create(o);
p.a =1;
p.b =2;
console.log(p.f()); //3

//五、get/set方法的this
function mod(){
    return Math.sqrt(this.re *this.re +this.im*this.im);
    }
var o={
    re:1,
    im:-1,
    get ph(){
        return Math.atan2(this.re,this.im)
        }
    };  
Object.defineProperty(o,'mod',{
    get:mod,enumerable:true,configurable:true
    }); 
console.log(o.mod,o.ph);//1.4142135623730951 2.356194490192345

//六。构造器中的this
function MyClass(){
    this.a = 23;
    }
var o = new MyClass();//实例化一个空对象o 原型链指向MyClass
console.log(o.a);//23 当对象只有基本类型时 会把基本类型作为返回值

function C1(){
    this.a = 23;
    return{a:22};//当对象有返回方法 会吧方法作为优先返回值
    }
o1 = new C1();
console.log(o1.a);//22


//七、call/apply方法与this
function add(c,d){
    return this.a +this.b +c +d;
    }   
var o={a:1,b:2};
console.log(add.call(o,2,3));   //8  .call(o,arg1,arg2..)
console.log(add.apply(o,[4,5]));//12 .apply(o,[arg1,arg2..]);

function bar(){
    console.log(Object.prototype.toString.call(this));//[object Number]
    }   
bar.call(7);
*/

//八、bind方法与this
function f(){
    return this.a;
    }   
var g= f.bind({a:'test'});
console.log(g());               //test

var o={a:21,f:f,g:g};
console.log(o.f(),o.g());       //21 "test"
</script>
</body>
</html>

相关文章

  • JavaScript 04 (do...while循环/for

    js循环,js循环嵌套,js do...while循环,js的for循环,js中的break,js中的contin...

  • iOS原生&JS交互

    OC 获取js中的key OC调用js方法 JS调用OC中不带参数的block JS调用OC中带参数的block ...

  • JS 对象

    JS对象 JS对象的意义和声明 在JS中,对象(OBJECT)是JS语言的核心概念,也是最重要的数据类型。在JS中...

  • 单引号和双引号

    JS中 js中单引号和双引号的区别(html中属性规范是用双引号,js中字符串规定是用单引号)(js中单引号区别和...

  • js中的this

    一句话,call的第一个参数 看几个例子例1. 这里的this是什么?不要靠猜,是call的第一个参数,不知道去看...

  • js 中的this

    首先js中函数的this在函数被调用时总是指向一个对象(this对象是在运行时基于函数的执行环境绑定的) 然后 它...

  • JS中的this

    JS中的this 众所周知,JS中this的代表的是当前函数调用者的上下文。JS是解释性的动态类型语言,函数在调用...

  • js中的!!

    !!一般用来将后面的表达式强制转换为布尔类型的数据(boolean),也就是只能是true或者false。 var...

  • js中的this

    目标:js中this的指向? 问题的引出 指出this指向什么 js中函数的三种调用形式 func(p1, p2)...

  • JS中的this

    初学JavaScript经常被this绕晕,所以我总结一下JavaScript中的this。首先声明本文讨论的是非...

网友评论

      本文标题:Js中的this

      本文链接:https://www.haomeiwen.com/subject/wahiottx.html