JavaScript中的this

this的指向


函数内部

function log() {
     //指向全局对象
    console.log(this);
}
对象中的方法

var tom = {
    name: "tom",
    walk: function () {
        //指向调用方法的对象
        return this.name + " have an apple";
    }
};
构造函数

function Cat(name, age) {
    //指向新对象
    this.name = name;
    this.age = age;
    this.eat = function () {
        return this.name + " eat fish";
    }
}
apply

function Point(x, y) {
    this.x = x;
    this.y = y;
}
var point0 = new Point(1, 1);
Point.prototype.move = function (a, b) {
    this.x += a;
    this.y += b;
};
var circle0 = {x: 1, y: 1, r: 1};
//指向apply的第一个参数
point0.move.apply(circle0, [8, 8]);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值