Skip to content

Instantly share code, notes, and snippets.

@danielpataki
Last active October 9, 2016 08:28
Show Gist options
  • Select an option

  • Save danielpataki/d8038c332948fbb10aaad64ce3eee56c to your computer and use it in GitHub Desktop.

Select an option

Save danielpataki/d8038c332948fbb10aaad64ce3eee56c to your computer and use it in GitHub Desktop.
Thinking In Javascript: Objects
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() );
{
name : 'Daniel Pataki',
awesomeness: 9000,
writesArticles: true
}
<!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>
console.log( book.timeToRead() );
console.log( book['timeToRead']() );
jQuery.ajax({
url: 'http://twitter.com/api',
type: 'post',
dataType: 'json',
complete: function() {
alert( "Yay, I've sent a request to Twitter" );
}
})
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 );
}
}
console.log( book.type );
console.log( book['title'] );
var author = book.author;
console.log( book[author] );
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>' );
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