=
' followed by the default value.
/** add 1 hour to startDate, if the caller
* doesn't supply the second parameter */
function addHour(startDate, hours = 1){
return new Date(startDate.getTime()+hours*3600*1000);
}
function myAwesomeFunction(){
return 'learning JavaScript is funny';
}
const anonymous = function(){
return 'learning JavaScript is funny';
}
this
-property. The this
-property is assigned, after the function is bound to an object. Unbounded functions don't have a this
-property. This can let to tricky bugs, as you can see in the following listing.
class Children{
constructor(birthday, surName){
this.birthday = birthday;
this.surName = surName;
}
getBirthdayAndName(){
const getFullName = function(){
return `Johannes ${this.surName}`;
}
return {birthday: this.birthday, fullName: getFullName()};
};
}
const children = new Children(new Date('2010-04-03T08:00:00'), 'Gerbershagen');
console.log(children.getBirthdayAndName());
this
-property of the function getFullName
isn't inherited from the surrounding method and for this reason it is undefined.this
property. They use the this
-property from the surrounding method and they are the right approach for stateless functions not bound to any object. Arrow functions are declared like this:
const arrowFunction = (arg1, arg2) => {
//implementation
};
class Children{
constructor(birthday, surName){
this.birthday = birthday;
this.surName = surName;
}
getBirthdayAndName(){
const getFullName = () => {
return `Johannes ${this.surName}`;
}
return {birthday: this.birthday, fullName: getFullName()};
};
}
const children = new Children(new Date('2010-04-03T08:00:00'), 'Gerbershagen');
console.log(children.getBirthdayAndName());
this
-property from the surrounding method is used in function getFullName
.this
-property. They are suitable for the procedural programming style.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
11 | |
9 | |
7 | |
6 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 |