When working with JavaScript, if an inherited function is called, it may be printed

Can someone explain why JSON.stringify(this.Master.Func) is showing 'undefined' instead of function() { ... }?

The function executes when we add ().

Check out the JSfiddle demo here: http://jsfiddle.net/t4ngY/

CODE

var $ = {}; // declaring a global variable

var Master = 
    {
        property: 'Property',

        Func: function()
        {
            console.log('I am Func inside Master');
        },

        PassToGlobal: function()
        {
            $.master = this;
        }
    };

Master.PassToGlobal();

var Slave =
    {
        Master: $.master,

        ShowFunc: function()
        {
            console.log(JSON.stringify(this.Master.Func)); //returns undef
            this.Master.Func(); //prints `I am Func inside Master`
        }
    }

Slave.ShowFunc();

Answer №1

To view the functionality text, all you need to do is execute the toString method as shown below:

console.log(this.Main.Function.toString()); 

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

Is it possible to trigger a function dynamically upon checking a checkbox?

I’ve been working on a simple string formatting app using React, diving into hooks as I go. Right now, I’m tackling the part where I need to trigger specific text formatting functions based on checkbox selections. Let's say, if the lowercase chec ...

Is there a way to identify the subdocument ids that have been updated in Mongoose when performing an

Is there a reliable way to retrieve the _id of subdocuments that are inserted into an array within my document using doc.updateOne? I am concerned about getting incorrect _id values when multiple updates occur. This is my current approach, but I'm wo ...

Data binding in Vue does not function properly within functional components

Clicking the button will cause the number n to increase, but the UI will display it as constant 1. <script> let n = 1 function add() { console.log(n) return ++n } export default { functional: true, render(h, ctx) { return (<div> ...

What is the best way to alter the header in Django when a user is authenticated?

In my project, I have two headers: header.html and headersuccess.html. When a user is logged in, I need to change the header from header.html to headersuccess.html. How can I implement that? Here is an excerpt from my views.py file where I render loginsuc ...

What is the method for exporting a variable from a module in JavaScript?

Based on information from this forum post, it is possible to export variables from one module to another in JavaScript: // module.js (function(handler) { var MSG = {}; handler.init = init; handler.MSG = MSG; function init() { // Initialize t ...

Error: The XML parsing in ASP failed to find a root element at the specified location

When clicking the button, I have jQuery/Ajax code that is supposed to pass the value of a selected radio button to a controller action and open a detail page. However, I am encountering an error. When using Mozilla Firefox, the console displays: XML Par ...

Tips for converting an online HTML document into a string within an Angular framework

I'm currently facing an issue while trying to extract data from an online HTML document using Angular. The problem lies in encountering a cors error consistently. Here is the snippet of my code for reading the HTML document: loadParsingData(htmlToP ...

Guide to sending a reference to a child input element using VueJs

I am currently attempting to pass a ref in order to retrieve the value of the input (base-input component) upon submission. The two components are listed below. Despite having a console.log statement in handleSubmit, the email variable always appears as un ...

What is the most efficient way to substitute text within an HTML document?

Is there a way to switch between languages on a website to replace text on multiple pages with a simple button click? What is the most efficient method for achieving this? This code snippet only changes text in the first div. Is there a way to implement a ...

What is the best way to submit a form using AJAX and ensure that the form values are encoded in

After a successful transaction, I need to send the data to PayPal. I am aware that using $.post('https://www.paypal.com/cgi-bin/webscr', data); is not the correct approach because I want it to be submitted as if it were coming from a form action, ...

Transform JavaScript into Native Code using V8 Compiler

Can the amazing capabilities of Google's V8 Engine truly transform JavaScript into Native Code, store it as a binary file, and run it seamlessly within my software environment, across all machines? ...

Struggling with properly locating a string within an array

I have been attempting to search an array for a specific string using a For loop, iterating through each index of the array based on its length. The expected behavior is that if the string is found, it should display that string (which in this scenario is ...

Infinite loop readiness with JQuery

My current project involves preloading images and seamlessly fading them in once they are fully loaded using JQuery. To achieve this, I attempted to create an invisible image tag where the images would load before setting the source back to the original im ...

What is the best way to flatten a nested array of objects with lodash?

I'm trying to convert a JSON object into a different format using JavaScript and lodash: { "BidNumber": 2, "BidResult": 1, "BidAmount": "6756", "BidData": [ { "name": "JonSnow", "Data": "Standard data f ...

"Learn how to efficiently incorporate data into data-binding in VUE JS with just a

In my data binding, there is a string that reads as follows: bc-men,bc-men-fashion,bc-men-underwear I want to create an input field where I can enter "bc-some-category", click "Add", and have it appended to the end of the list with a comma in front. Her ...

Get the JSON array by decoding the JSON data in PHP

I have a JavaScript function that creates a JSON array and uses an AJAX post method. How should this JSON array be decoded on the PHP side? Here is my JavaScript function: var invoices = {invoice: [{ "customerName" : "John" ,"reciptionName" : "Doe" ,"dat ...

Run a C# void method from a JavaScript function in an ASPX web form

I am facing an issue with a button that triggers a delete action in a database. I have implemented a custom JavaScript function to create a dialog box for users to confirm if they want to proceed with the deletion. However, I am encountering difficulties i ...

What is the optimal method for transmitting data for a substantially large music playlist via HTTP?

I am currently in the process of developing an online music player. My main challenge lies in retrieving a comprehensive list of songs from the database and transmitting it to the user. The user should have the ability to create playlists on-the-go, hence ...

Utilize regular expressions in TamperMonkey to extract specific groups of text

I'm currently working on a TamperMonkey userscript that aims to identify URLs matching a specific pattern, visit these pages, extract relevant information, and then update the link for each URL with the extracted text. I'm facing some challenges ...

Implementing Dynamic Weight-based Pricing with CodeIgniter and AJAX

My shopping cart website utilizes ajax and Codeigniter to add products without reloading the page. Originally, I displayed a single net weight for each product. However, I recently switched to multiple net weights for the same product. Unfortunately, I am ...