简体   繁体   English

这个关键字在类方法中

[英]this keyword in a class method

var mC = function(map){
    var init = function(iMap){
        alert("Init " + this + " with");
    }
    init(map);
};
var m = new mC({});

why i am getting value of this as [object window]? 为什么我以[对象窗口]的形式获得价值? Is it a window object?? 它是窗口对象吗?

It's because init is not a "class method" - it's a function you're defining and calling inside a constructor. 这是因为init不是“类方法”-它是您在构造函数中定义和调用的函数。 That doesn't make it special of other functions in any way. 但这丝毫没有使其其他功能独树一帜。

You will need to call the init function in the context of the mC function's 'this': 您将需要在mC函数的“ this”的上下文中调用init函数:

init.call(this);

Or, you will need to make 'init' a member of this or this.prototype, which will automatically use the object it's a member of as 'this' 或者,您需要使“ init”成为this或this.prototype的成员,这会自动将其所属的对象用作“ this”

You may want to google about JavaScript's this keyword if this confuses you :) 如果这使您感到困惑,您可能想在Google上搜索JavaScript的this关键字

What else would you be expecting to get? 您还期望得到什么?

You defined init as a function and then called it in the global space so that is why you got what you did. 您将init定义为一个函数,然后在全局空间中对其进行调用,这就是为什么要完成您的工作的原因。 You didn't attach it to the mC class. 您没有将其附加到mC类。

Yes! 是! Because init is a variable of mC , it will share its scope (which currently is the global scope, which is also the window object). 由于initmC的变量,它将共享其作用域(当前是全局作用域,它也是window对象)。

However. 然而。 If you changed to the following: 如果更改为以下内容:

var mC = function(map){
    this.init = function(iMap){
        alert("Init " + this + " with");
    }
    this.init(map);
};
var m = new mC({});

Then this inside init would be a reference to your instance. 然后, this init内部init将引用您的实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在addeventlistener中使用此关键字调用javascript类方法 - Calling javascript class method using this keyword in addeventlistener 通过“this”关键字访问静态方法中的类属性 - Access a Class property within the static method by "this" keyword 在侧边javascript类中没有`function`关键字的异步方法声明 - async method declaration without `function` keyword in side javascript class 通过调用方法创建类的实例,而无需新的关键字 - Create instance of class by calling method, without new keyword 如何在使用此关键字的方法中设置类变量的状态 - How do I set the status of a variable of a class in a method that using this keyword 何时在React类组件中的方法之前使用static关键字 - When to use the static keyword before a method in a React class component 在addeventlistener中使用此关键字调用javascript类的方法 - calling javascript class's method using this keyword in addeventlistener 如何在 class 的方法内的事件侦听器中使用“this”关键字,而不会失去对 class arguments 和方法 ZDBC11CAA5BDA99F77E6FBDABDBDBDA99F77E6FBDABDBD77 的访问权限 - How to use “this” keyword inside event listener inside a method of a class without losing access to class arguments and method arguments? 如何在不使用 this 关键字的情况下访问 Javascript 中同一类的方法内的类的构造函数中定义的属性? - How can I access a property defined in a constructor of a class inside a method of the same class in Javascript without using this keyword? JavaScript类表示法和'this'关键字 - JavaScript class notation and the 'this' keyword
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM