Confused with JavaScript prototype
813 ワード
They are two simiar code below:
1.
I'm wondering why the first piece code 「user.co nstructor.prototype['sex']」return「undefined」、I think it would be「man」.
Can somebodl tell me?
--
Thanks for your comment Sky.
In the first implement、User.prototype.com.nstructor is modified to Object、not User itself.The refore user.co nstructor is equal to User.prototype、they ar all Object.Wecan solitype protors protrappect.
1.
function User(name) {
this.name = name;
}
User.prototype = { 'sex': 'man' };
var user = new User('Zhang');
alert(User.prototype['sex']); // man
alert(user.constructor.prototype['sex']); // undefined
alert(user.constructor.prototype.constructor === Object); // true
My problem:I'm wondering why the first piece code 「user.co nstructor.prototype['sex']」return「undefined」、I think it would be「man」.
Can somebodl tell me?
--
Thanks for your comment Sky.
In the first implement、User.prototype.com.nstructor is modified to Object、not User itself.The refore user.co nstructor is equal to User.prototype、they ar all Object.Wecan solitype protors protrappect.