JavaScript学习笔记(五)js技巧
1. 安全的类型检测 Object.prototype.toString.call(value) // '[object Array]' 2.作用域安全的构造函数 function Person(name) { if (this instanceof Person) { this.name = name } else { return new Person(name) } } 继承 function Polygon(sides) { if (this instanceof Polygon) { this.sides = sides this.getArea = function() { return 0 } } else { return new Polygon(sides) } } function Rectangle(width, height) { Polygon.call(this, 2) //这里只是简单用Polygon给this添加一些属性而已的操作 this.width = width this.height = height this.getArea = function() { return this.width * this.height } } Rectangle.prototype = new Polygon() //