Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

object methods in javascript

/// OBJECTS IN JAVASCRIPT
const testScore = {
  damon: 89,
  shawn: 91,
  keenan: 80,
  kim: 89,
};

Object.keys(testScore);  // gives all keys
Object.values(testScore); // gives all values
Object.entries(testScore); // gives nested arrays of key-value pairs

// YOU CAN USE ( FOR-IN )  LOOP FOR ITERATION OVER OBJECTS
for (let person in testScore) {...} 

// WE CAN'T DIRECTLY USE ( FOR-OF ) LOOP IN OBJECTS BUT WE CAN DO Like THIS:
for(let score of Object.values(testScore)){
  	console.log(score)  // 89 91 80 89 
}

Comment

Object method in javascript

const student = {
    id: 101,
    major: "Mathamatics",
    money: 5000,
    name: "Abrar",
    subjects: ["English", "Economics", "Math 101", "Calculus"],
    bestFriend: {
        name: "Kundu",
        major: "Mathematics"
    },
    takeExam: function () {
        console.log(this.name, "taking exam");
    },
    treatDey: function (expense, tip) {
        this.money = this.money - expense - tip;
        return this.money
    }
}
student.takeExam();
//Output: Abrar taking exam
const remaining1 = student.treatDey(900, 100);
const remaining2 = student.treatDey(500, 50);
console.log(remaining1);
console.log(remaining2);
//Output: 4000,3450
Comment

define methods of objects in javascript

objectName.methodname = functionName;

var myObj = {
  myMethod: function(params) {
    // ...do something
  }

  // OR THIS WORKS TOO

  myOtherMethod(params) {
    // ...do something else
  }
};
Comment

methods of object js

create()
keys()
values()
entries()
assign()
freeze()
seal()
getPrototypeOf()
Comment

JavaScript Object Methods

const person = {
  firstName: "John",
  lastName: "Doe",
  id: 5566,
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
};
Comment

JavaScript Object Methods

const person = {
    name: 'Sam',
    age: 30,
    // using function as a value
    greet: function() { console.log('hello') }
}

person.greet(); // hello
Comment

object methods

//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 :: jquery disable all forms 
Javascript :: javascript foreach break 
Javascript :: isfunction javascript 
Javascript :: angular autocomplete displaywith 
Javascript :: node get current user 
Javascript :: can we call ajax inside ajax success 
Javascript :: extract content from string html 
Javascript :: how to change html element in javascript 
Javascript :: int val javascript 
Javascript :: keyup in jquery 
Javascript :: find smallest length string in an array js 
Javascript :: javaScript (DOM) HTML Element by Id 
Javascript :: javascript function from string 
Javascript :: password validation in regex 
Javascript :: adding a timer in a quiz game 
Javascript :: css vw not working on mobile 
Javascript :: javascript loop through array backwards 
Javascript :: what is side effect 
Javascript :: hex string to decimal string javascript 
Javascript :: creating react app using npx 
Javascript :: how to run a bash script with node js 
Javascript :: gsheet query select remove header 
Javascript :: csv upload with react 
Javascript :: Download Node Module With NPM 
Javascript :: javascript array filter elements greater than 
Javascript :: model nodejs 
Javascript :: isChecked radio button jQuery 
Javascript :: let var 
Javascript :: jest render target container is not a dom element 
Javascript :: document.getelementbyid 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =