Created
March 17, 2016 07:47
-
-
Save npras/4c04a23a958d3306c48a to your computer and use it in GitHub Desktop.
javascript function definition and calling syntax
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
// 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