weird part 57–58— function constructor, ‘new’ and ‘.prototype’

blossom0417
1 min readOct 12, 2018

--

#function constructor — a method is inside function

function Person(firstname, lastname){console.log(this)
this.firstname = firstname;
this.lastname = lastname;
this.getFullName = function(){
return this.firstname + ' '+ this.lastname;
}

console.log('this function is invoked.')
}
var april = new Person('April','Doe')

#.prototype

function Person(firstname, lastname){console.log('this',this)
this.firstname = firstname;
this.lastname = lastname;
console.log('this function is invoked!')
}
Person.prototype.getFullName = function(){
return this.firstname + ' ' + this.lastname;
}
var john = new Person('john','doe');
console.log(john)
console.log('fullname',john.getFullName())
.prototype

See the difference between ‘the method getFullName ’ and ‘prototype.getAge’?

#function constructor

  • the first letter of function constructor name — capital letter! ‘Person’

# work a lot of Date object

#references

https://youtu.be/12QRecyUd28?t=139

--

--

No responses yet