Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
javascript function definition and calling syntax
// this is function definition.
// you can make a function take zero or more arguments (also called as parameters)
// This Dog fn takes one argument: 'name'
function Dog(name){
return "hello, my name is: " + name;
}
// calling the Dog function
Dog('tommy'); // this will return "hello, my name is: tommy"
// you can check this in chrome js console with:
console.log(Dog('tommy'));
////////////////////////////////////////
//// Objects
var person = {}; // an empty object. We can add anything to it later on like below
// you can add 'attribute' properties to an object
person.age = 28;
person.name = 'prasanna';
// you can check the value of 'person' now
console.log(person); // you should see something like: {age: 28, name: 'prasanna'}
// you can also add 'function' properties to an object
// let's teach our person object to say his name
person.sayName = function() {
return "My name: " + this.name; // notice the use of 'this'
};
// you can now ask the person object to say his name!
console.log( person.sayName() ); // "My name: prasanna" will be the output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment