Last active
January 31, 2020 03:24
-
-
Save AhmedHelalAhmed/82cf544b98b316d1f7b83e0ac0446d1c to your computer and use it in GitHub Desktop.
add function to array of objects in js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "scripts": [], | |
| "showConsole": true | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let printName = function() { | |
| console.log(this.name); | |
| }; | |
| let items = [ | |
| { | |
| name: "ali" | |
| }, | |
| { | |
| name: "print" | |
| }, | |
| { | |
| name: "abc" | |
| } | |
| ]; | |
| items.forEach(item => { | |
| item.print = printName; | |
| }); | |
| items[2].print(); | |
| items[1].print(); | |
| items[2].print(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment