Javascript的痛點就是物件導向編程,上一篇介紹了Javascript的兩種繼承方式:Javascript 進階 繼承,這篇使用一個例子來展示js如何物件導向編程,以及如何基於類實現繼承。
1、利用物件導向的寫法,實現下面這個功能,即時更新資料的一個例子:
2、使用對上面類的繼承,完成下面的效果:<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGltZyBzcmM9"http://www.2cto.com/uploadfile/Collfiles/20140609/20140609091555422.gif" alt="\">
好了,不多說,js的訓練全靠敲,所以如果覺得物件導向不是很紮實,可以照著敲一個,如果覺得很紮實了,提供了,可以自己寫試試。
1、第一個代碼:
/** * Created with JetBrains WebStorm. * User: zhy * Date: 14-6-7 * Time: 下午4:55 * To change this template use File | Settings | File Templates. *//** * @param id * @param value * @param parentEle 父元素 * @constructor */function PlaceFieldEditor(id, value, parentEle){ this.id = id; this.value = value; this.parentEle = parentEle; this.initValue = value ; this.initElements(); this.initEvents();}PlaceFieldEditor.prototype = { constructor: PlaceFieldEditor, /** * 初始化所有元素 */ initElements: function () { this.txtEle = $(""); this.txtEle.text(this.value); this.textEle = $(""); this.textEle.val(this.value); this.btnWapper = $(""); this.saveBtn = $(""); this.cancelBtn = $(""); this.btnWapper.append(this.saveBtn).append(this.cancelBtn); this.parentEle.append(this.txtEle).append(this.textEle).append(this.btnWapper); this.convertToReadable(); }, /** * 初始化所有事件 */ initEvents: function () { var that = this; this.txtEle.on("click", function (event) { that.convertToEditable(); }); this.cancelBtn.on("click", function (event) { that.cancel(); }); this.saveBtn.on("click", function (event) { that.save(); }); }, /** * 切換到編輯模式 */ convertToEditable: function () { this.txtEle.hide(); this.textEle.show(); this.textEle.focus(); if(this.getValue() == this.initValue ) { this.textEle.val(""); } this.btnWapper.show(); }, /** * 點擊儲存 */ save: function () { this.setValue(this.textEle.val()); this.txtEle.html(this.getValue().replace(/\n/g,"
")); var url = "id=" + this.id + "&value=" + this.value;// alert(url); console.log(url); this.convertToReadable(); }, /** * 點擊取消 */ cancel: function () { this.textEle.val(this.getValue()); this.convertToReadable(); }, /** * 切換到查看模式 */ convertToReadable: function () { this.txtEle.show(); this.textEle.hide(); this.btnWapper.hide(); }, setValue: function (value) { this.value = value; }, getValue: function () { return this.value; }};
引入到頁面代碼:
<script type="text/javascript" src="jquery-1.8.3.js"></script> <script type="text/javascript" src="PlaceFieldEditor.js"></script> <script type="text/javascript"> $(function () { $("ul li").each(function () { new PlaceFieldEditor($(this).attr("id"), "請輸出成績...", $(this)); }); }); </script> 嗯,代碼就不詳細說了,都比較簡單,使用了jQuery,如果不喜歡可以使用原生js,本人比較喜歡把jQuery當作js的工具使用。
2、第二個的js代碼:
/** * Created with JetBrains WebStorm. * User: zhy * Date: 14-6-7 * Time: 下午5:34 * To change this template use File | Settings | File Templates. */function PlaceAreaEditor(id, value, parentEle){ PlaceAreaEditor.superClass.constructor.call(this, id, value, parentEle);}extend(PlaceAreaEditor, PlaceFieldEditor);PlaceAreaEditor.prototype.initElements = function (){ this.txtEle = $(""); this.txtEle.text(this.value); this.textEle = $(""); this.textEle.text(this.value); this.btnWapper = $(""); this.saveBtn = $(""); this.cancelBtn = $(""); this.btnWapper.append(this.saveBtn).append(this.cancelBtn); this.parentEle.append(this.txtEle).append(this.textEle).append(this.btnWapper); this.convertToReadable();};
寫了PlaceAreaEditor繼承了PlaceFieldEditor,然後複寫了initElements方法,改變了text為textarea。
extend的方法,上一篇部落格已經介紹過:
/** * @param subClass 子類 * @param superClass 父類 */function extend(subClass, superClass){ var F = function () { }; F.prototype = superClass.prototype; //子類的prototype指向F的_proto_ , _proto_又指向父類的prototype subClass.prototype = new F(); //在子類上儲存一個指向父類的prototype的屬性,便於子類的構造方法中與父類的名稱解耦 使用subClass.superClass.constructor.call代替superClass.call subClass.superClass = superClass.prototype;}最後頁面代碼:
<script type="text/javascript" src="jquery-1.8.3.js"></script> <script type="text/javascript" src="PlaceFieldEditor.js"></script> <script type="text/javascript" src="com.zhy.extend.utils.js"></script> <script type="text/javascript" src="PlaceAreaEditor.js"></script> <script type="text/javascript"> $(function () { $("ul li div").each(function () { new PlaceAreaEditor($(this).attr("id"), "請留言...", $(this)); }); }); </script> - 我要改劇本,不讓~~
- 懸崖上有橋麼,有?沒有~
- 你敢打壞我的燈?不租~
好了,結束~~ 上面的例子是根據孔浩老師的例子修改的,感謝孔浩老師,孔老師地址:www.konghao.org。孔老師錄製了很多Java相關視頻,有興趣的可以去他網站學習!
代碼或者講解有任何問題,歡迎留言指出。