Option 1:
<script>
function Gadget(name, color) {
this.name = name;
this.color = color;
this.whatAreYou = function(){
return 'I am a ' + this.color + ' ' + this.name;
}
}
var user = new Gadget('David', 'White');
console.log(user.whatAreYou());
</script>
Option 2:
<script>
function Gadget(name, color) {
this.name = name;
this.color = color;
}
Gadget.prototype = {
whatAreYou: function(){
return 'I am a ' + this.color + ' ' + this.name;
}
}
var user = new Gadget('David', 'White');
console.log(user.whatAreYou());
</script>
Question:
Option 1, I put method into function()
; Option 2, I added method through prototype
, Both of them work. But is there any differnce in these two options when create objects?