the primary way JavaScript defines its "this" binding

As I delve into a book, it mentions that this within a function can be established in four different ways: default, implicit, explicit binding, and through the use of the new syntax. Regardless of the method, this is determined at the time of the function call. Let's examine an example.

Upon invoking jajca(), one would anticipate this to be set via default binding, meaning when jajca() is called, this should already reference the object created using the new syntax. However, perplexingly, I noticed that within jajca, this actually points to the window object instead of the expected object created with new.

function jajca() {
   this.musicFolderPath = "value2"; // remains unassigned to the correct this.musicFolderPath
}

function Music(){
  this.musicFolderPath;
  jajca(); // music.musicFolderPath is undefined
  // jajca.call(this); music.musicFolderPath is set correctly
}

    var music = new Music();
    //music.musicFolderPath remains undefined

Why isn't this being set as anticipated? It appears that when explicit binding is utilized, this does indeed refer to the object created with the new keyword as expected.

Answer №1

When I invoke the function jajca(), I anticipate that the default binding will set 'this' to refer to the object created using the new keyword. However, for some unknown reason, instead of pointing to the object as expected, 'this' inside jajca() points to the window object.

The crux of the matter is that each time you call a different function, the reference of 'this' representing an object will be "reset". According to the rules governing 'this', when you execute obj.bar(), 'this' within bar will be obj. This explains why the value of 'this' remains constant when you use this.baz(): it signifies that the 'this' in baz should equal the current 'this' value.

This is a complex topic that your textbook likely delves into better than I can explain succinctly on StackOverflow. By keeping this principle in mind while studying, you'll find it easier to comprehend things.

In this scenario, yes, you would need to employ jajca.call(this) to ensure that 'this' inside jajca corresponds to your desired target.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Combining various functions into a single button

I am currently working on creating a full-screen menu that functions like a modal. Everything seems to be working fine, except for the fadeOut animation. Can someone please help me understand what is causing issues with my scripts/codes? I want the content ...

What is the process for eliminating transparency from the overlay feature while utilizing Dojox/Standby?

Currently, I'm using a spinner image to indicate loading while retrieving data from the backend. However, the spinner widget creates a translucent overlay over the control. Is there a way to disable this translucency so that only the spinner is visibl ...

Understanding the performance of video in threejs when using getImageData

Edit; Check out the working codepen (you'll need to provide a video file to avoid cross-origin policy) https://codepen.io/bw1984/pen/pezOXm I'm currently trying to adapt the amazing rutt etra example from to utilize video (using threejs), but ...

Exploring Commitments in React

I'm currently navigating the world of promises and finding it challenging to grasp! My main focus right now is setting up an authentication system for my application. RegisterPage The handleSubmit function in my RegisterPage looks like this: handl ...

Access rejected due to X-Frame-Options: "http://test.test.net/Feedback/Create?appId=TestApp" prohibits cross-origin framing in MVC5

Currently, I am developing a website that is hosted on my company's internal network and can only be accessed from within the network. As a result, cross-domain requests are not a concern for me. As part of this website, I have included a "Provide Fe ...

Executing Multiple Ajax Requests in Jquery

I have a jQuery script that is designed to submit data to the database: //delegated submit handlers for the forms inside the table $('#issue').on('click', function (e) { ...

Mapping the Way: Innovative Controls for Navigation

Currently, I am utilizing the HERE maps API for JavaScript. However, I would like to customize the design of the map controls similar to this: Below is an example for reference: HERE EXAMPLE Is it feasible to achieve this customization? ...

"Encountering an error in Vue.js when trying to dynamically access nested arrays: push function not

My goal is to have two buttons displayed when a user uploads data: one for old products and one for new products. When the user clicks on either button, the corresponding products will be uploaded as 'old_product' or 'new_product'. Howe ...

Implementing CSS counter-increment with jQuery

Is there a way to use jQuery to set the CSS counter-increment attribute on ".demo:before" even though jQuery cannot access pseudo elements directly? I recall seeing a suggestion on Stack Overflow about using a data attribute and then referencing that value ...

Leveraging AJAX to fetch an array or string for controller utilization in CakePHP 2

In my current scenario, I am dealing with multiple banner ads on a page and I need to track how many times each one is displayed. For example, let's consider banners with IDs 1, 2, 3, 4, and 5. Every time the page refreshes, banners with IDs 1, 2, an ...

Please provide the text input for the specified version numbers (1.1, 1.2, 3.0, etc.)

I'm currently working on a form that includes a section for searching by version number: <label class="formLabel">Version</label> <html:text property="valueVersion" styleClass="value" tabindex="11"/> <br/& ...

Switch out image with Jquery when hovering, and revert to default on mouse out

Despite the numerous times this question has been asked, I am struggling to find a solution that fits my specific case. Let's dive into it. My navigation setup involves using Bootstrap nav pills to navigate through content. The structure looks like t ...

Reactjs rating field material ui Jest tests are encountering failures

Currently, I am facing a challenge while writing a Jest test for the rating UI field in Material UI. In the past, I had successfully implemented something similar to this: it('testing rendering and submission of a basic Formik form', async () = ...

Modifying material in Three.js

My method for selecting geometry involves using the numbers on my keyboard, as shown below: if(keyboard.pressed("1")){ obj = torus; } This allows me to toggle their visibility: if(keyboard.pressed("a")){ THREE.SceneUtils.traverseHierarchy( obj, fu ...

obtain a Javascript variable within a Jade template by using inline code

script. var hide_section = 'block'; .title(style='display:#{hide_section}') I received an undefined value, any thoughts on why? Could it be because #{hide_section} is trying to access a variable from the back-end, such as the cont ...

Ways to incorporate AJAX into a JavaScript function

I'm still learning about Ajax and I have a JavaScript function that retrieves the value selected from a list within an li element. However, every time this function is executed, the page reloads. I want to incorporate Ajax into this function so that t ...

Encountering an issue with displaying data fetched from omdbapi, receiving [object Object] instead

Everything is working perfectly in my code except for the mysterious appearance of [object Object] whenever I perform a search. I've combed through my code multiple times, but I just can't seem to locate the source of this issue. It would be grea ...

Passing asynchronous data from method1 to method2 without impacting the functionality of the script responsible for fetching the asynchronous data in method1

When working with TypeScript, I encountered an issue while trying to invoke an external script called SPCalendarPro within a private method that asynchronously fetches data. The script is invoked in the following manner: private _getSPCalendarPro() { con ...

React Native ScrollView with a custom footer that adjusts its size based on the content inside (

I've implemented the code below to ensure that the blue area in image 1 remains non-scrollable when there is sufficient space, but becomes scrollable when space is limited. However, I'm facing an issue where the ScrollView component is adding ext ...

Having trouble choosing files on Mobile devices, whether it's IOS or Android

I'm having trouble getting an image upload to change the background of a table automatically. It works fine on Desktop computers, but not on IOS or Android. Does anyone have any advice on how to make it work across all devices? Thanks in advance. cons ...