在Java腳本中是否可以使用OO方法? 我使用JavaScript在服務器端和客戶端使用node.js.Currently我使用查詢到CRUD操作,而不是查詢是否可以使用DTO'S保存數據庫中的數據?Java腳本中面向對象的方法
3
A
回答
0
是的,你可以使用原型繼承來模擬它。規則是非常不同的,因爲語言是原型驅動的,你需要做一些關於原型鏈等的研究,但最後它變得非常有用。
檢查從EventEmitter繼承的Node核心中的東西。它們有一個名爲util.inherits的內置函數,它具有用於ECMA更高版本的實現。它看起來像這樣:
/**
* Inherit the prototype methods from one constructor into another.
*
* The Function.prototype.inherits from lang.js rewritten as a standalone
* function (not on Function.prototype). NOTE: If this file is to be loaded
* during bootstrapping this function needs to be rewritten using some native
* functions as prototype setup using normal JavaScript does not work as
* expected during bootstrapping (see mirror.js in r114903).
*
* @param {function} ctor Constructor function which needs to inherit the
* prototype.
* @param {function} superCtor Constructor function to inherit prototype from.
*/
exports.inherits = function(ctor, superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
};
一個例子使用的是Stream類:
https://github.com/joyent/node/blob/master/lib/stream.js#L22-29
var events = require('events');
var util = require('util');
function Stream() {
events.EventEmitter.call(this);
}
util.inherits(Stream, events.EventEmitter);
在CoffeeScript的,類編譯成一個稍微不同的組碼,其歸結爲__extends功能。我相信這樣會有更多的跨瀏覽器兼容性,但我沒有具體回想誰不支持Object.create。
var __hasProp = Object.prototype.hasOwnProperty, __extends =
function(child, parent) {
for (var key in parent) { if (__hasProp.call(parent, key))
child[key] = parent[key]; }
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
};
+0
OK喬什我會嘗試。 – Baskar
相關問題
- 1. 面向對象的Java腳本
- 2. Javascript - 對象表示法腳本vs面向對象的腳本
- 3. android中的面向對象的方法
- 4. 面向對象的Perl腳本
- 5. winbatch windows腳本語言面向對象?
- 6. java界面 - 面向對象
- 7. 面向對象的Java,arraylist
- 8. 基本面向對象設計java
- 9. AngularJS面向對象的方法
- 10. 不是面向對象的buildMaxHeap方法
- 11. 面向對象的調用方法php
- 12. 面向對象的方法使用ColdFusion
- 13. 面向對象的方法C#
- 14. 面向對象的方法橋樑
- 15. Java腳本數組反向對象對象錯誤
- 16. 面向對象編程方法問題
- 17. 德爾福面向對象方法
- 18. 面向對象設計方法
- 19. Python 2.7面向對象__init__方法
- 20. PHP:面向對象和方法
- 21. python面向對象編程方法
- 22. Java - 面向對象表
- 23. 面向對象編程 - 有關WindowsForm方法的基本信息
- 24. 在非面向對象的php腳本中調用yii類
- 25. 面向對象的方法來處理複雜的方法
- 26. 面向對象的方法是什麼? (或你的方法?)
- 27. 未定義的方法regular_user_path(誓言面向對象的方法)
- 28. 什麼錯與非面向對象的方法來介紹面向對象的方法
- 29. 找不到對象方法 - Perl腳本
- 30. 對象旋轉和當前面向方向的移動; Java
請閱讀道格拉斯Crockford的*的JavaScript:好的部分」 – ebohlman
OK ebohiman我會嘗試 – Baskar