// 第一种写法: // 也是我通常的写法,不过我感觉这样的写法看上去太过于分散,不像一个整体,总感觉不优雅function Person(){ this._name = 'test'; ...}Person.prototype = { sayHello: function(){ console.log(this.name); }}// 第二种写法: // 前几天又看了别人写的关于实现 jquery 框架的写法,这种写法不知道有什么优势??// 我感觉就是 把 简单的 复杂化了 ...// 看上去比较分散var jQuery = function(selector , context){ return new jQuery.fn.init(selector , context);}jQuery.fn = jQuery.prototype = { init: function(selector , context){ // 这段看不太懂,应该是返回 实例的意思 return this; } ....}jQuery.fn.init.prototype.init.prototype = jQuery.fn;// 第三种写法: // 我感觉是最优雅的写法,不好的地方是在于成员变量共用,导致不支持并发使用...// 构造函数可以通过 prototype 实现 实例化后的对象继承;这个无需实例化,该如何实现继承??var Tool = { name: 'test' , sayName: function(){ console.log(this.name); }}// 第四种写法:// 看上去比较比较分散...function Move(){ this.name = 'test';}(function(fn){ fn.sayName = function(){ console.log(this.name); };})(Move.prototype);以上是我所知的js面相对象的写法,我非常想使用第三种写法,可我不知道怎么解决他存在的问题?? 也麻烦大家对比下这几种写法的优劣。核心的还是,如何使第三种写法成为通用写法(我打算以后就用这种写法,若是能够解决它存在的问题的话)??
添加回答
举报
0/150
提交
取消