简体   繁体   English

对象文字的此关键字

[英]this keyword for object literal

Hi In the example below I am importing MeasurementObject using: import MeasurementObject from './app/..etc' 嗨在下面的示例中,我使用以下方法import MeasurementObject from './app/..etc'

When I call an instance of MeasurementObject.getScrollToValue() 当我调用MeasurementObject.getScrollToValue()的实例时

The this inside the method MeasurementObject.getScrollToValue() refers to the executing context. MeasurementObject.getScrollToValue()方法内部的this引用执行上下文。 As a result this.getURL() is undefined. 结果this.getURL()是未定义的。 How can I get it to to refer to the current context of MeasurementObject and hence call this.getURL() . 我如何获取它以引用MeasurementObject的当前上下文并因此调用this.getURL()

const  MeasurementObject  = {

    getType(){
        return "a"
    }

    getURL(){
        return "b";
    }

    getScrollToValue(){
        return this.getURL();
    }
}

export default MeasurementObject;

The fact that you're exporting them makes no difference. 您正在导出它们的事实没有区别。

MeasurementClass is a constructor function with 3 methods defined on the MeasurementClass.prototype . MeasurementClass是一个构造函数 ,在MeasurementClass.prototype上定义了3种方法。 getType , getURL and getScrollToValue getTypegetURLgetScrollToValue

For you to use the class, you would have to instantiate the class using the new keyword. 为了使用该类,您将必须使用new关键字实例化该类。 (new MeasurementClass()).methodName(...)

vs.

MeasurementObject is an object literal with 3 methods defined directly on the object. MeasurementObject对象文字,具有直接在对象上定义的3种方法。 getType , getURL and getScrollToValue getTypegetURLgetScrollToValue

Using the object is just a matter of calling obj.methodName() 使用对象只是调用obj.methodName()

when you export a class without instantiate it, all the properties you define are set on the prototype. 当您导出一个没有实例化的类时,您定义的所有属性都在原型上设置。 and you dont have a context, you need to instantiate and you then are able to use "this". 并且您没有上下文,则需要实例化,然后可以使用“ this”。

Now as for your example the "this" inside your function referring to the object correctly however you are not returning it. 现在以您的示例为例,函数中的“ this”正确引用了对象,但是您没有返回它。

const MeasurementObject = {

    getType(){
        return "a"
    },

    getURL(){
        return "b";
    },

    getScrollToValue(){
        return this.getURL();
    }
}


var a = MeasurementObject.getScrollToValue()
console.log(a)

When you define an object like that your already working on an instance and the properties are set on the object not its prototype. 当您定义这样的对象时,您已经在实例上工作并且属性是在对象上设置的,而不是其原型上。

update 更新

When you do this: 执行此操作时:

class MeasurementClass {

    getType(){
        return "a"
    },

    getURL(){
        return "b";
    },

    getScrollToValue(){
        return this.getURL();
    }
}

what you get is something like: 您得到的是这样的:

function MeasurementClass(){}
MeasurementClass.prototype.getType = function(){ return "a" }
MeasurementClass.prototype.getURL = function(){ return "b"; }
MeasurementClass.prototype.getScrollToValue = function(){ return     this.getURL(); }

And then when you try accessing it without instantiating MeasurementClass.getScrollToValue 然后,当您尝试访问它而不实例化MeasurementClass.getScrollToValue

You are trying to access something that doesn't exist. 您正在尝试访问不存在的内容。

However when you instantiate the function all of those prototype properties are inherited to the instance so: 但是,当实例化该函数时,所有这些原型属性都将继承到该实例,因此:

const measurementClass = new MeasurementClass();

measurementClass will be an instance of MeasurementClass and will inherit all its prototype. measurementClass将是MeasurementClass的实例,并将继承其所有原型。

Hope that helps a little. 希望能有所帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM