Two approaches for one single object

I'm trying to figure out why this particular code works

// .......................................................

var character = { 
  name: 'Joni', 
  type: 'blond',
  sayName: function() { 
    return this.name; 
  }, 

  sayType: function() { 
    return this.type; 
  } 
}; 
document.write(character.sayName() + "</br>"); 
document.write(character.sayType());

// .......................................................

but for some reason, this one doesn't work

// .......................................................

var character = { 
  name: 'Joni', 
  type: 'blond',
  sayName: function() { 
    return this.name; 
  }, 

  sayType: function() { 
    return this.type; 
  } 
}; 
document.write(character.sayName() + "</br>"); 
document.write(character.sayType());

// .......................................................

Any help would be appreciated. Thank you!

Answer №1

Make sure to add a semi-colon at the end of the "var hero" statement and include the necessary commas in your code.

var hero = { 
  name: 'Joni', 
  type: 'blond',
  sayName: function() { 
    return this.name; 
  }, // <<--- Don't forget the comma here

  sayType: function() { 
    return this.type; 
  } 
};  // <<--- Remember to add the semi colon!
document.write( hero.sayName()+ "</br>"); 
document.write( hero.sayType());

To prevent these mistakes in the future, consider using JSLINT. Simply go to jslint.com, input your code, and it will help you identify any errors.//

Answer №2

What sets the two apart is the introduction of an "enter" command before executing the saytype function in the subsequent code block. With the exception of a lone comma, there doesn't appear to be any other barriers to its functionality.

Answer №3

After reviewing the code, it appears that there is a missing comma after the sayName function in the block of code provided. Interestingly, despite a new line character being present between two blocks of code, there was no discernible difference beyond this. It is worth noting that both code blocks were lacking this crucial comma.

var hero = { 
  name: 'Joni', 
  type: 'blond',
  sayName: function() { 
    return this.name; 
  }, // Missing Comma
  
  sayType: function() { 
    return this.type; 
    } 
} 
document.write(hero.sayName() + "</br>"); 
document.write(hero.sayType());

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

Troubleshooting problems with sending data in Jquery Ajax POST Request

I've spent a considerable amount of time searching for a solution to why my post request isn't sending its data to the server. Oddly enough, I can successfully send the request without any data and receive results from the server, but when attemp ...

Styling Result received from Web API call within an array

I am currently working on a function call that retrieves the result of an SQL query in an array format. app.get('/alluser', (req, res) => { console.log("All Users") user.getAllUsers(function (err, result) { if (!err) ...

Guide on how to automatically direct users to a page upon successful login in ReactJS

How can I redirect to the homepage after a successful login in ReactJS? Also, how can I display an error message when a user enters incorrect credentials? I have attempted the following code, but it does not successfully redirect to the homepage or show ...

Navigating Sockets with Navigator in React Native

Every time I encounter the error message undefined is not an object (evaluating this.props.socket.on). In my code, I define the socket in the index.ios.js file as shown below: class Main extends Component { constructor(props) { super(props); ...

The property cannot be set for an undefined element in Jquery UI slider tabs

There seems to be some extra space at the bottom of the slider tabs between the navigators and content. Additionally, I am facing a challenge in making the page responsive as the content size does not adjust when I resize the window. To see the changes, I ...

Error: doc.data().updatedAt?.toMillis function is not defined in this context (NextJs)

I encountered an error message while trying to access Firestore documents using the useCollection hook. TypeError: doc.data(...)?.updatedAt?.toMillis is not a function Here is my implementation of the useCollection Hook: export const useCollection = (c, q ...

Issue with Colorbox plugin preventing ASP.NET button from triggering postback

Currently utilizing the colorbox modal plugin (http://colorpowered.com/colorbox/) I'm facing an issue with a simple form placed in a master page - the submit button is unresponsive and doesn't trigger any action. It seems like several users are ...

Refresh the content with an embedded iframe

I am attempting to update the body content by removing all existing content and inserting an iframe: function create_custom_iframe(target){ var iframe = document.createElement('iframe'); iframe.setAttribute('id', 'target_u ...

React / Next.js Rendering Problem, Data Discrepancy Between Client and Server

A new feature has been added to a webpage where an image background and a city name are displayed on top of it. The data file that generates these image backgrounds and city data consists of a standard array of objects. The challenge lies in dynamically l ...

Tips for splitting a container of specific height into sections measuring 80% and 20%

I am working on a container with a fixed position that I want to split into two halves: 80% and 20% at the bottom. This is what I want it to look like: Click here to see the image. Note: The layout should adjust itself when the window is resized. You c ...

The AJAX request is not successful because it is encountering a ReferenceError due to attempting to assign a value to an

My attempt to trigger a modal using dynamic data from the database with an AJAX function is encountering issues. Upon clicking the button to open the modal, I encounter this error: ReferenceError: assignment to undeclared variable PostRPC in category.ph ...

Error encountered in jQuery 3.3.1: Unable to append a row to the tbody of a table

As I attempt to dynamically insert a row into an HTML table when a click event occurs using JQuery 3.3.1 and Bootstrap 4, I encounter a slight issue. HTML: <table id='tblAddedCallsign' class='table table-striped'> </table> ...

submit django form when a checkbox is checked

tml: <div id="report-liveonly"> <form action="." id="status" method="POST">{% csrf_token %} <p>{{SearchKeywordForm.status}}Only display LIVE reports</p> </form> </div> I am facing an issue while trying to submit ...

Avoiding Memory Leaks and Managing JS Heap Size While Polling Large Amounts of Data Every 5 Seconds with Vuex

I'm currently working on a Vue application that utilizes Vuex for state management. Within this application, I have several store modules set up at the root level. Periodically, every 5 seconds, I retrieve a large amount of data and update the store s ...

A customizable and adaptable Tetris-inspired CSS design perfect for any screen size

If we imagine having a block like this: <div class="block"></div> There are different sizes of the block: <div class="block b1x1"></div> <div class="block b2x1"></div> <div class="block b1x2"></div> For i ...

JavaScript or HTML can be used to store external embed PDF files in a cache

I have a PDF file from an external source embedded on this page. Although it displays correctly, I am concerned about the file being downloaded each time the page is visited. Would using an object tag instead help to cache the PDF and prevent repeated do ...

The content within the hyperlinked text

<ul class="Buttons"> <li><a href="#" onclick="myFunc(); return false;">Accept</a></li> <li><a href="#" onclick="myFunc(); return false;">Reject</a></li> <li><a href="#" onclick="myF ...

Excessive CPU usage caused by a patch in jQuery dealing with regular expressions

Working on a project developed by an unknown individual has presented some challenges. Without any means of contact with this person, I noticed that the browser's CPU consumption spikes significantly upon loading the page. Upon further investigation, ...

I am looking to create a PHP script that restricts access to individuals under the age of 18 based on the current

I'm looking for a way to use Php, javascript, or ajax to restrict access for individuals under 18 years old based on the current date. Any suggestions on how I can achieve this? Could someone please review my code for calculating age onblur or onSubm ...

Transfer the values from identical textboxes to jQuery, and subsequently to a PHP page for saving them to a database

Here is a list of textboxes that I have: ` <table id="div1" style="width:100%;"> <tr> <td> <label>Question Text</label> </td> <td colspan="5"> ...