loading...

0

15.原型内部属性

javascript读完大概需要2分钟

  • 发布时间:2017-07-26 20:31 星期三
  • 刘伟波
  • 622
  • 更新于2017-07-26 20:31 星期三

原型内部属性

    <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>


你可能感兴趣的文章

    发表评论

    评论支持markdown,评论内容不能超过500字符
    关于技术问题或者有啥不懂的都可以来 我的视频空间 提问, 推荐作者总结知识点 前端知识体系, 感謝支持!