What causes the difference in accessing internal object member variables between JavaScript and Vue.js?

Currently, I am in the process of learning Javascript and vue.js. I am encountering some confusion when it comes to accessing member variables of an internal object within an object in Vue.js, as it differs from traditional Javascript.

Consider the JavaScript object below:

let obj = {
    name : 'n',
    ob : {
        c : 'char',
        d : 'int'
    },
    printcd : function()
    {
        console.log(this.ob.c);
        console.log(this.ob.d);
    }
}

In this example, we access 'c' and 'd' through the 'ob' property (as in this.ob.c). However, when we look at the equivalent code in Vue.js:

ap = new Vue(
{
    el: "#app",
    data:
        {
            name:
                {
                    first: 'First',
                    last:'Last',
                    age: 30
                }
        },
    computed:
        {
            getName: function() {
                return this.name.first + ' ' + this.name.last;
            }
        }
 }

Here, inside the 'getName()' method, we are accessing 'first' and 'last' directly without using 'data'. Instead of using 'this.data.name.first', we simply use 'this.name.first'. If anyone could explain why the mechanism for accessing properties is different in Vue.js, I would greatly appreciate it.

Answer №1

When you input new Vue, you are not receiving the Vue object directly. Instead, it serves as a blueprint for creating that object. The Vue object contains main elements extracted from the data, methods, and computed parts of the blueprint. (Possibly also from the directives and components sections, although they are not commonly accessed directly.)

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

What is the best way to incorporate npm packages into my projects?

Lately, I've been heavily relying on nodejs, but I keep running into the same issue. With so many projects available and a plethora of npm packages to choose from, it's frustrating that every time I try npm install --save some-package, I struggle ...

Generate a menu submenu allowing for interactive toggling and dynamic visualization of expandable/collapsible sections upon clicking, featuring visually

Looking for assistance in creating a dynamic menu with sub-menus that can expand upon clicking. The goal is to have an expandable menu structure where the user can click to expand or collapse certain sections. However, my current code is not functioning co ...

Tips for incorporating a visible marker beside the dropdown arrow

When developing my React application with React hooks forms, I have a select component where I need to include a clear indicator 'x' icon next to the dropdown icon. The current select component code is: <Form.Control size="sm" as=&q ...

Visual Matrix and Directional Controls

I'm currently learning JavaScript and I've put together this script from different sources to help me grasp the basics. I prefer to keep it simple and would appreciate explanations if it gets too complex. Don't assume I know too much about i ...

Having trouble verifying a phone number using the awesome-phonenumber npm package?

I recently developed some JavaScript using the awesome-phonenumber npm package. index.js const yargs = require('yargs'); var PhoneNumber = require( 'awesome-phonenumber' ); const argv = yargs .usage('Usage: $0 -n [num]' ...

Unexpected behavior with jQuery AJAX Callback function:

When attempting to extract data from the success part of an AJAX call, I implemented a callback function for this purpose. Here is the code snippet: var data2; $(function () { function callback(data) { console.log(data); data2 = JSON.parse(data ...

There are no settings available for Google API, including access tokens, refresh tokens, API keys, or refresh handler callbacks

Attempting to establish a connection to Google search console API utilizing OAuth2 const {google} = require('googleapis'); const auth = new google.auth.OAuth2( YOUR_CLIENT_ID, YOUR_CLIENT_SECRET, YOUR_REDIRECT_URL ); const searchconsole = ...

Steps for adding SVGs to a <div> element

While there are numerous solutions on Stack Overflow detailing how to append an SVG in a div, none of them have proven successful for me. This could be due to my lack of experience with SVGs. Therefore, I am generating an SVG on my node server and the ...

Can html-webpack-plugin be configured to create <style> elements from CSS files?

I am managing a static site with Vue and Webpack. In my project, I have a file named style.css containing global CSS rules which I import using import './styles.css' in my index.js file. Additionally, I have some .vue files that generate their o ...

What are the reasons behind the min and max range method not providing accurate results?

I am in need of a method that can verify if a given value falls within the valid range of -064.000000 to -180.000000 and 142.000000 to 180.000000. The structure of my ranges object is as follows: "ranges": { "range1": { "min& ...

I am having trouble with my jQuery login function not properly connecting to the PHP file

Hey there, I've been working on creating a login system from scratch by following an online tutorial. The initial Javascript is functioning properly as it detects errors when the inputs are empty. However, once I enter text into the input fields and c ...

Refresh the page to view multiple modals in one window on W3

I've been attempting to repurpose the W3 image modal code in order to open multiple modals on the same page. I managed to come up with a solution based on answers I found here, but unfortunately, it only seems to work if I refresh the browser page (si ...

Building a JavaScript application with Node.js and MySQL to seamlessly interact with both offline and online databases

As I develop a JavaScript web-app, my plan is to utilize Node.js for connecting the app with an existing MySQL database. My initial question pertains to the structure of the Node code: should it be written in the same .js file as my application or kept se ...

Currency unique to a specific culture

Recently, I encountered an issue while working on a website that involved using JavaScript code to format currency values. Here is the snippet of code that was causing the problem: UsdAmount.toLocaleString(siteCulture, {style: 'currency', ...

Setting the "status" of a queue: A step-by-step guide

I created a function to add a job to the queue with the following code: async addJob(someParameters: SomeParameters): Promise<void> { await this.saveToDb(someParameters); try { await this.jobQueue.add('job', ...

Having issues with the POST method in node.js and express when connecting to a MySQL database

My GET method is functioning perfectly I have a database called stage4 and I am attempting to insert values into it from a frontend page The connection is established, I'm using Postman to test it first, but it keeps returning a "404 error" which is ...

Experiencing difficulties installing the MEAN stack

I've been attempting to set up the MEAN stack by following a tutorial on Bossable website. I'm using Webstorm and MongoDB for this installation. Unfortunately, I'm facing some issues and encountering errors. Every time I try to connect to l ...

Retrieve the value of a JSON string

I'm having trouble accessing a json value in this example. No alert is being produced. Can you help me figure out what the problem is? This is the JSON data I am working with: { "response": [ { "id": "0", "elementName": "osname", "isEqual": t ...

Express is throwing an error indicating that the specified route cannot be found. Is there a way to dynamically

After dynamically adding routes and sending a post request through Postman, I encountered a "not found" error message. In the app, a 404 is logged next to the endpoint, indicating that Express cannot locate it. However, I know that I added the router dynam ...

The placeholder attribute for input types does not display consistently across all browsers

I am experiencing an issue with the placeholder text in my input field. <input type="text" name='linkLabel{{index}}' autocomplete="off" class="input-large tight-form-url last remove-cross" required="required" placeholder="{{'linkLabel&ap ...