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 :: javscript async function 
Javascript :: jquery sweet popup 
Javascript :: javascript foreach call specific value in array 
Javascript :: for in in javascript 
Javascript :: how to get a random item from an array javascript 
Javascript :: how to build a string javascript es6 
Javascript :: es6 hashset 
Javascript :: javaScript Math.log() Method 
Javascript :: double click react 
Javascript :: Material-ui add circle icon 
Javascript :: how to break from map in javascript 
Javascript :: initiate node js app 
Javascript :: sort array by field 
Javascript :: buttons js before submit 
Javascript :: how can we access the data from array object in javascript 
Javascript :: Generate random phone numbers in POSTMAN 
Javascript :: uppercase each word javascript 
Javascript :: images node backend server 
Javascript :: js loop array back 
Javascript :: mongoose objectid parse 
Javascript :: declaring variable react hooks 
Javascript :: read more/less button with smoth expand 
Python :: python int64index 
Python :: django version check 
Python :: plt figsize 
Python :: dataframe to csv without ids 
Python :: python argparse ignore unrecognized arguments 
Python :: jupyter notebook print all rows dataframe 
Python :: python min in dictionary 
Python :: how to add legend to python plot 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =