user interface - Need explaination of this Javascript code -
<p id="demo"></p> <script> function employee(name, jobtitle, born) { this.name=name; this.jobtitle=jobtitle; this.born=born; } var fred = new employee("fred flintstone", "caveman", 1970); document.getelementbyid("demo").innerhtml = employee.name; </script>
when execute code output : employee if execute employee.jobtitle in place of employee.name like:
document.getelementbyid("demo").innerhtml = employee.jobtitle
i output : undefined. why ?
when ask employee.name
in fact asking name of function. appears new functionality that has appeared in es6, while may work in current browsers have started implementing es6 standards, i'll bet last fiver doesn't work in older browsers ie8, because don't support es6.
the reason employee.jobtitle
returns undefined
because there no native property called jobtitle
part of function.prototype
unlike name
.
remember, employee
constructor, , it's fred
that's object instance in case. fred.name
, fred.jobtitle
give property values of object expect.
Comments
Post a Comment