15.原型内部属性
原 javascript读完大概需要2分钟
- 发布时间:2017-07-26 20:31 星期三
- 刘伟波
- 622
- 更新于: 2017-07-26 20:31 星期三
loading...
原 javascript读完大概需要2分钟
<script> //引用类型中都有prototype原型,该属性指向对象 var obj={ //如果没有Student.prototype.country="中国" 要强制这样让Student constructor; constructor:Student,//强制让stu的构造方法为Student 建议这样写 country:"中国" }; function Student(name,age) { this.name=name; this.age=age; this.study= function () { console.log("我叫"+this.name+",今年"+this.age+"岁,我爱学习。"); }; } // Student.prototype=obj;//覆盖了Student原有的原型对象 Student.prototype.country="中国";//在Student原有的原型添加一个country属性,使用的是本来的原型对象 另一种写法 // console.log(Student.prototype);//Object // console.log(obj.__proto__);//object var stu=new Student("张三","22");//内部创建了一个__proto__ 都指向了prototype // var stu2=new Student("李四","22");//内部创建了一个__proto__ 都指向了prototype console.log(stu.constructor);//function Student(name,age) {} 创建构造函数的方法 //如果Student.prototype=obj console.log(stu.constructor);// function Object() { [native code] } __proto__指向了obj的构造方法了 </script>
发表评论: