How can one access the property "this.var" within a function declared within an object

I stumbled upon this snippet of code

var person = {
    name: "joseph",
    age: 33,
    favSong: function killingInTheTameOf(){
        this.lyrics = "Those who died are justified";
    }
};

document.write(person.lyrics); //doesn't work

My curiosity lies with the this.lyrics variable,
what is the significance of this?
how can I access it?
what exactly does this refer to? Is it person?
I have come across explanations about this before, but none seem to address this particular scenario.

Answer №1

To generate the property lyrics, ensure you invoke favSong.

var person = {
    name: "joseph",
    age: 33,
    favSong: function killingInTheTameOf(){
        this.lyrics = "Those who died are justified";
    }
};

person.favSong();
document.write(person.lyrics); //works perfectly

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

Despite awaiting them, promises are not resolving synchronously

I have a function that retrieves location information and returns a promise. I use mobx to manage the store, updating the this.locationStoreProp and this.hotel.subtext properties. public fetchPropertyLocation(some_input_params): Promise<any> { ...

Retrieve the names contained within TD elements using the console

I always enjoy experimenting with new things. Take a look at the https://lodash.com/docs/4.17.15 lodash documentation site where you'll find a menu on the left side featuring all available functions. Is there a way to extract the names of these functi ...

Can you please explain the function of this JavaScript code for the Isotope filter?

I am struggling to grasp the functionality of a section of vanilla JS code related to the Isotope filter. You can find the original code here. var buttonGroups = document.querySelectorAll('.button-group'); for (var i = 0; i < buttonGroups.le ...

Exceeding the maximum update depth can occur if a component triggers a setState function within the useEffect hook

useEffect(() => { const retrieveUserInfo = database().ref('/User/' + user.uid ).on('value', snapshot => { setComplete(snapshot.val().Complete); setUserProfile(snapshot.val().User); setStartYear(snapshot.val(). ...

Track changes and save only the updated elements in an array

Currently, I am utilizing Angular for the front end, C# for the backend, and an Oracle database for a project with a company. Within the grids provided to me, there are more than 120 records that users can edit individually. My dilemma lies in identifyin ...

Utilizing ReactJS and Promises: Incorporating catch within then calls

I've implemented an action creator in my codebase (which returns a function as I'm using redux-thunk). Within this function, I utilize the dispatch method and chain the then and catch methods. Here's a snippet of how it looks: export functi ...

Using client-side routing to handle GET requests

Encountering a similar scenario as Julian: MVC - Route with querystring I am struggling to figure out how to handle a form submission with a GET request, utilizing defined routes and values from the form. (EDIT: facing a nearly identical issue as Julian ...

The event handler has now reverted back to its original undefined state

I'm having trouble with the following code snippet: function dnd(){ } var ele = document.getElementById("relative"); ele.addEventListener("click",dnd,false); document.write(ele.onclick); When I check the output, it shows as undefined. I expect it to ...

Enhance your website with unique and custom fonts using

I am utilizing this repository. How can I incorporate custom fonts into my project? I have created a folder named "fonts" within the assets directory and placed my fonts there. fonts.scss @font-face { font-family: 'Lato'; src: url('../ ...

Different approach to handling response interceptors in AngularJS framework

Is there an alternative to $httpProvider.responseInterceptors since it has been discontinued in AngularJS V1.3? The interceptors that were functioning with Angular JS 1.2 are no longer operational in version 1.3 var angularErrorHandling = angular.module( ...

RN TypeScript is handling parameters with an implicit any type

My React Native (RN) application includes the following code snippet: handleTextChange = e => { this.setState({ value: e }) } I am using TypeScript (TS) and it's giving me a warning saying, "parameter 'e' implicitly has 'any&apos ...

Utilize Jquery and MVC 5 to create a reoccurring partial view

I have a button that triggers the loading of a partial view containing a table with all the categories in my system. <input type="button" value="Load Categories" id="btnLoadCategories" /> This loading process is done via a partial view which is in ...

The process of making a pop-up modal instead of just relying on alerts

Attempting to change from using an alert to a pop-up with a simple if statement, but encountering some issues. Here is the current code: if(values == ''){ $('body').css('cursor','auto'); alert("Blah Blah..." ...

How can I address multiple buttons with various events using jQuery?

I am new to learning jQuery and I'm currently working on some exercises. However, I've run into an issue with two buttons in the DOM that are supposed to perform different actions. I can't seem to figure out how to assign different functions ...

Why is this <div> element refusing to budge according to my jQuery commands?

Currently embarking on a jQuery coding challenge where I am attempting to maneuver a circle around the page in a square pattern. Numerous methods have been tested with no success thus far. While I would like to showcase my various attempts, it would prove ...

CoffeeScript:: I can't understand why the function body returns when using ajax

Hey there, I'm new to Coffeescript and have a question regarding Ajax. jQuery -> api = getId: -> res = [] $.ajax dataType: "jsonp" url: "http://localhost:3004/videos.json" success: (data) => ...

Using jQuery to send a JSON-encoded request containing multiple objects

I need help with a project I am working on. I currently have a running mariaDB server with a database and some tables, and I want to update the table entries using a web interface. To do this, I have created a PHP script as follows: <?php include &apo ...

Split up the author page description with a new line break for better readability on WordPress

I have a unique plugin that transforms the Biographical Info editor into a standard editor interface. However, I am facing an issue where line breaks/new rows created in the backend are not visible on the front end, and I'm unsure of where the problem ...

Using the Airbnb style guide in conjunction with NextJS

Incorporating the Airbnb style guide into my NextJS 13.4.9 project is a priority for me. When setting up a NextJS application, the prompt to enable ESLint arises. Opting to say "yes" is typically the recommended approach, as it allows for running npm run l ...

send the user to a different page while transferring post data using Javascript

Currently, I have an HTML form that utilizes the post method to send data to a PHP page for processing into a MySQL database. However, before the user submits the form, there is a button that allows them to view a PDF of the entered data on a new page. Whi ...