JavaScript JavaScript - Best way to always access a specific Object

If I create an Object as follows:

window.Something = {
    X: function() {
        // statements...
    }
};

What is the best way to access this object without using this?

Example:

If I call the X property by using:

window.addEventListener("DOMContentLoaded", window.Something.X);

In this case using this will access window and not Something. I thought at this method but I think it is a bad practice.

window.Something = {
    X: function() {
        var This = window.Something;

        // statements...
    }
};

There is an implicit way to always access the real object that created the property?

Answer:1

Nobody has pointed out why this is occurring.

According to MDN:

When attaching a handler function to an element using addEventListener(), the value of this inside the handler is a reference to the element. It is the same as the value of the currentTarget property of the event argument that is passed to the handler.

In other words, this refers to the window object because the event listener is attached to the window object. For instance, if you attached the event listener to an input element, this.name would refer to the name attribute of the input element that the event was attached to.

Example Here

window.Something = {
  name: 'Some name',
  X: function() {
    console.log(this.name); // 'test'
  }
};

document.getElementById('target').addEventListener('input', Something.X);
<input id="target" name="test" />
Answer:2

Use bind() to call the handler with the appropriate this:

window.addEventListener("DOMContentLoaded", window.Something.X.bind(window.Something));
Answer:3

I need pathname (www.my-site.com/this-part/and-this-part/etc/) in JS/jQuery but I need it as string not as object. In other words I need $_SERVER['REQUEST_URI']; in JS/jQuery. I've tried: var ...

I need pathname (www.my-site.com/this-part/and-this-part/etc/) in JS/jQuery but I need it as string not as object. In other words I need $_SERVER['REQUEST_URI']; in JS/jQuery. I've tried: var ...

I have a web application that uses ExtJS 4 that incorporates a Ext.grid.EditorGridPanel. One of the columns of the Ext.grid.EditorGridPanel has a combobox editor that I would like to disable ...

I have a web application that uses ExtJS 4 that incorporates a Ext.grid.EditorGridPanel. One of the columns of the Ext.grid.EditorGridPanel has a combobox editor that I would like to disable ...

I have drawn a complex path in my browser (Firefox) as a background, using Javascript-generated SVG elements and attributes, and would like to superimpose icons that consist of multiple SVG shapes. ...

I have drawn a complex path in my browser (Firefox) as a background, using Javascript-generated SVG elements and attributes, and would like to superimpose icons that consist of multiple SVG shapes. ...

  1. javascript generate svg
  2. javascript generate svg image
  3. javascript generate svg path
  4. svg generator js
  5. generate png from svg javascript
  6. generate svg using javascript

This question is a follow up to what I previously asked (HTML Form Number Type Unique User Input) but now I'm making the form more dynamic. The following form asks users to list there 3 favorite ...

This question is a follow up to what I previously asked (HTML Form Number Type Unique User Input) but now I'm making the form more dynamic. The following form asks users to list there 3 favorite ...

  1. html form number input
  2. html form number only
  3. html form number range
  4. html form number increment
  5. html form number default value
  6. html form number decimal
  7. html form number min max
  8. html form number max length
  9. html form number only input
  10. html form number type
  11. html form number float
  12. html form number picker
  13. html form number length
  14. html form number positive
  15. html form number
  16. html form number step
  17. html form number max
  18. html form number limit
  19. html form number format
  20. html form number size