Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Add New Properties to a JavaScript Object

var myDog = {
  "name": "Happy Coder",
  "legs": 4,
  "tails": 1,
  "friends": ["freeCodeCamp Campers"]
};

myDog.bark = "woof";
// or
myDog["bark"] = "woof";
Comment

javascript Adding Properties And Methods in an Object

// constructor function
function Person () {
    this.name = 'John',
    this.age = 23
}
// creating objects
let person1 = new Person();
let person2 = new Person();
// adding property to person1 object
person1.gender = 'male';
// adding method to person1 object
person1.greet = function () {
    console.log('hello');
}
person1.greet();   // hello

// Error code
// person2 doesn't have greet() method
person2.greet();
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript Adding Element to the Outer Array 
Javascript :: JavaScript, numbers are primitive data types 
Javascript :: javascript Add Symbol as an Object Key 
Javascript :: javascript Rename in the module 
Javascript :: javascript copy array using spread operator 
Javascript :: JavaScript Destructuring - Before ES6 
Javascript :: convert dom object to string 
Javascript :: javascript Assigning to a getter-only property is not allowed 
Javascript :: JavaScript Code Blocks 
Javascript :: Save multiple Child 
Javascript :: disable SerializableStateInvariantMiddleware and ImmutableStateInvariantMiddleware middlewares for large redux stores 
Javascript :: datatable all items in one line 
Javascript :: promise limit time 
Javascript :: mongoose schema index of multiple columns 
Javascript :: set angle in phaser 
Javascript :: phaser increment x layers 
Javascript :: phaser create animation on sprite 
Javascript :: react-native-fbsdk-next 
Javascript :: Clean way to remove text and keep div inside a div jquery 
Javascript :: Move capital letters to the beginning 
Javascript :: site:stackoverflow.com two api calls dependent on each other js 
Javascript :: Adding A Property To BuiltIn Class In Javascript 
Javascript :: graphql nested schema 
Javascript :: do while loop js 
Javascript :: queryselector j 
Javascript :: javascript no decimal places 
Javascript :: node js post multipart/form-data 
Javascript :: javascript run function 
Javascript :: mvc asp.net partial view from js 
Javascript :: js code 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =