对象原型(__proto__)和构造函数原型对象(prototype)里面都有一个属性constructor,constructor我们称为构造函数,因为它指向的是构造函数本身. constructor主要用于记录该对象引用于哪个构造函数,它可以让原型对象重新指向原来的构造函数. 下面这个例子可以说明: function Student(name, age) { this.name = name this.age = age } …
首先用一个例子指出来constructor存在形式. function Fruit(){ } var f=new Fruit(); console.log(f.constructor);//打印出Fruit() 由上面的代码我们总结出结论1:上面的代码在控制台可以看出constructor是指向构造器Fruit的引用. function Fruit(){ this.name="水果"} //var f=new Fruit(); function Apple(){this.name=&q…
/** * Created by Administrator on 2016/12/26. */ /* var box; alert( typeof box); box是Undefined类型,值是undefined,类型返回的字符串undefined var box=true; alert( typeof box); box是Boolean类型,值是true,类型返回的字符串boolean var box=''; alert( typeof box); //box是String类型,值是'',…
我们先想想我们用js最后要怎样实现面向对象的编程.事实上我们必须用上原型链这种东西. 我们的父类superType有属性和方法,并且一些能被子类subType继承,一些能被覆盖,但是丝毫不会影响到父类.那么我们可以这样定义:父类子类同时拥有独立的属性,但是共享父类被继承的方法. 继承是靠prototype实现的. class实现 我们最理想化的js面向对象es6已经帮我们做出来了,我们可以先试下es6的,然后再用es5实现这种效果. class Person { constructor(age)…