Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

JavaScript Object Properties

person.firstname + " is " + person.age + " years old.";
Comment

object properties

//There are 2 main ways to create an Object in JavaScript

//The First Method:
let firstPlayer = new Object();

//You can add properties like this:
firstPlayer.name = 'Player 1';
firstPlayer.level = 3;
firstPlayer.inventory = ['a half-eaten cracker', 'some pocket lint', 'a flimsy tree branch'];
firstPlayer.description = 'Don't mention Player 2 around them, they'll get angry...';

//You can create methods like this:
firstPlayer.checkLevel = function() {
    console.log(`${this.name} is currently... Level ${this.level}!`);
    //The "this" keyword refers to the object
}

firstPlayer.checkLevel();
//This will print "Player 1 is currently... Level 3!" to the Console


//The Second Method:
let secondPlayer = {
    
    //You can add properties like this:
    name: 'Player 2',
    level: 20,
    inventory: ['a health potion', 'a sack of gold coins', 'a sturdy steel blade'],
    description: 'Better than Player 1 in every way possible.',
    
    //You can create methods like this:
    checkLevel: function() {
        console.log(`${this.name} is currently... Level ${this.level}!`);
        //The "this" keyword refers to the object
    }
}

secondPlayer.checkLevel();
//This will print "Player 2 is currently... Level 20!" to the Console

//And that's it! Decide what method you prefer and start making some Objects!
Comment

PREVIOUS NEXT
Code Example
Javascript :: js reverse 
Javascript :: en eternal gloden braid 
Javascript :: chart-js-2 
Javascript :: react-particles-js not working 
Javascript :: correct way to push into state array 
Javascript :: react call bind apply 
Javascript :: usecontext 
Javascript :: print js example 
Javascript :: strapi v4 populate 
Javascript :: Add Text Inside Of React Component 
Javascript :: dataset javascript 
Javascript :: react usecallback hook 
Javascript :: express sendfile root path 
Javascript :: React S3 Bucket 
Javascript :: variables in js class 
Javascript :: add object to array javascript 
Javascript :: environment variable to debug knex 
Javascript :: javascript split a string 
Javascript :: javascript forloop 
Javascript :: get all a tags javascript 
Javascript :: cy.contains 
Javascript :: store object in input value 
Javascript :: create http request 
Javascript :: importing sha256 hashing algorithm 
Javascript :: add int to string javascirpt 
Javascript :: multi filtering react 
Javascript :: update state in useState hook 
Javascript :: promises chaining 
Javascript :: js setinterval run immediately 
Javascript :: javscript loop array 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =