Last active
October 9, 2016 08:28
-
-
Save danielpataki/d8038c332948fbb10aaad64ce3eee56c to your computer and use it in GitHub Desktop.
Thinking In Javascript: Objects
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
| function Tweet( text, username ) { | |
| this.text = text; | |
| this.username = username; | |
| this.display = function() { | |
| return this.text + ' - ' + this.username; | |
| } | |
| } | |
| var tweet_1 = new Tweet( 'I love dogs, they are awesome!', '@danielpataki' ), | |
| var tweet_2 = new Tweet( 'Coconut juice is the best thing to drink.', '@danielpataki' ), | |
| console.log( tweet_1.text ); | |
| console.log( tweet_2.display() ); |
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
| { | |
| name : 'Daniel Pataki', | |
| awesomeness: 9000, | |
| writesArticles: 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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>My First JS Boilerplate</title> | |
| </head> | |
| <body> | |
| <script type='text/javascript'> | |
| var me = { | |
| name: 'Daniel', | |
| greeting: function() { | |
| alert('Hello ' + this.name ) | |
| } | |
| }; | |
| me.greeting(); | |
| </script> | |
| </body> | |
| </html> |
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
| console.log( book.timeToRead() ); | |
| console.log( book['timeToRead']() ); |
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
| jQuery.ajax({ | |
| url: 'http://twitter.com/api', | |
| type: 'post', | |
| dataType: 'json', | |
| complete: function() { | |
| alert( "Yay, I've sent a request to Twitter" ); | |
| } | |
| }) |
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
| var book = { | |
| type: 'book', | |
| title: 'Going Postal', | |
| author: 'Terry Pratchett', | |
| length: 324, | |
| 'Terry Pratchett' : { | |
| type: 'author', | |
| "first-name": 'Terry', | |
| "last-name": 'Pratchett' | |
| }, | |
| timeToRead: function() { | |
| return ( this.length * 2.5 / 60 ); | |
| } | |
| } |
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
| console.log( book.type ); | |
| console.log( book['title'] ); | |
| var author = book.author; | |
| console.log( book[author] ); |
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
| function tweet( tweet ) { | |
| return tweet.text + ' - ' + tweet.username; | |
| } | |
| var tweets = [ | |
| { | |
| text: "I love dogs, they are awesome!", | |
| username: '@danielpataki' | |
| }, | |
| { | |
| text: "Coconut juice is the best thing to drink.", | |
| username: '@danielpataki' | |
| }, | |
| { | |
| text: "Writing a JS tutorial just now", | |
| username: '@danielpataki' | |
| } | |
| ]; | |
| document.write( '<ul>' ); | |
| tweets.map( function( tweet_object ) { | |
| document.write( '<li>' + tweet( tweet_object ) + '</li>' ); | |
| }) | |
| document.write( '</ul>' ); |
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
| function Tweet( text, username ) { | |
| this.text = text; | |
| this.username = username; | |
| this.display = function( prefix = '', suffix = '' ) { | |
| return prefix + this.text + ' - ' + this.username + suffix ; | |
| } | |
| } | |
| var tweets = [ | |
| new Tweet( 'I love dogs, they are awesome!', '@danielpataki' ), | |
| new Tweet( 'Coconut juice is the best thing to drink.', '@danielpataki' ), | |
| new Tweet( 'Writing a JS tutorial just now', '@danielpataki' ), | |
| ]; | |
| document.write( '<ul>' ); | |
| tweets.map( function( tweet ) { | |
| document.write( tweet.display( '<li>', '</li>' ) ); | |
| }) | |
| document.write( '</ul>' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment