123456789101112131415161718192021222324252627282930313233343536373839404142 |
- var square = x => x * x;
- var user = {
- name: 'Pedro',
- // Arrow functions do not bind the 'this' keyword
- sayHi: () => {
- console.log(`Hi! I am ${this.name}`);
- },
- // Traditional functions do bind the 'this' keyword
- sayHiAlt: function() {
- console.log(`Hi! I am ${this.name}`);
- },
- /* This could also be written like this:
- sayHiAlt() {
- console.log(`Hi! I am ${this.name}`);
- }
- */
- // Arrow functions also don't bind the arguments array
- printArgs: () => {
- // This is not a local arguments array, but global arguments array
- // be careful!!
- console.log(arguments);
- },
- printArgsAlt() {
- // This is a local arguments array from function call
- console.log(arguments);
- }
- };
- user.sayHi();
- user.sayHiAlt();
- // Global args get printed
- user.printArgs(3, 'sad tigers', 3, ' wheat dishes');
- // { '0': 3, '1': 'sad tigers', '2': 3 ... } local arguments array as object
- user.printArgsAlt(3, 'sad tigers', 3, ' wheat dishes');
- console.log(square(9));
|