if (typeof Array.isArray === "undefined") {
Array.isArray = function (arg) {
return Object.prototype.toString.call(arg) === "[object Array]"
};
}
Javascript继承-借用构造函数
var Widget = function (name) {
this.messages = [];
};
Widget.prototype.type = 'Widget';
var SubWidget = function (name) {
Widget.apply(this, Array.prototype.slice.call(arguments));
this.name = name;
};
SubWidget.prototype = Widget.prototype;
var sub1 = new SubWidget('foo');
var sub2 = new SubWidget('bar');
sub1.messages.push('foo');
sub2.messages.push('bar');
console.log(sub1.messages);//foo
console.log(sub2.messages);//bar
Javascript原型-封装
var Dialog = (function () {
function Dialog() {
}
Dialog.prototype = {
init: function () {
console.log("ok");
}
};
return Dialog;
}());
var d = new Dialog();
d.init();//ok
通过闭包修正函数的上下文(浏览器不支持解决方案)