OOP programming in Javascript with Node.js

I actually play with Javascript, which makes a small game, and I would like to implement what I found at http://www.crockford.com/javascript/inheritance.html , something similar:

ZParenizor.method('toString', function () { if (this.getValue()) { return this.uber('toString'); } return "-0-"; }); 

I can not find links to the library used to create such a development. Any ideas? Otherwise, I am looking for a good library that will help my OOP developments.

thank

Edit:

I am looking for OOP solution / library for Node.js. Please note that I am new to Node.js

+16
Jun 14 '11 at 0:25
source share
7 answers

2 months later

Maybe you need a library, ES5 is verbose, so I created pd

Original answer

I am looking for an OOP solution / library for Node.js.

You do not need a library. You have ES5 .

JavaScript does not have classic OOP. He prototypes OOP.

This means that you only have objects. The only things you can do with objects are expansion, manipulation, and cloning.

Manipulation

 var o = {}; o.foo = "bar"; 

Extend

 var o = someObject; Object.defineProperties(o, { "foo": { value: "foo" }, "bar": { value: "bar" } "method": { value: function () { } } } 

Clone

 var o = someObject; var p = Object.create(o); 

Clone and extension

 var o = someObject; var p = Object.create(o, { "foo": { value: "foo" }, "bar": { value: "bar" } "method": { value: function () { } } } 

It is important to understand how Object.create , Object.defineProperty and Object.defineProperties .

The cloning operation is not actually cloned. This creates a new object from the plan. A project is an object. It places the project in [[Prototype]] . [[Prototype]] lives in the .__proto__ , which I will use for demonstration.

 var o = {}; var p = Object.create(o); p.__proto__ === o; // true var q = Object.create(p); q.__proto__.__proto__ === o; var r = Object.create(q); r.__proto__.__proto__.__proto__ === o; 

Disclaimer: .__proto__ out of date. Do not use it in code. It uses it to verify debugging and to verify health.

The main thing is that access to properties from o to r must go three levels up the prototype chain, and this becomes expensive. To solve this problem, and not clone random objects, you must clone specific drawings (and you must have one project for each object).

 // Parent blueprint var Parent = (function _Parent() { // create blank object var self = Object.create({}); // object logic return self; }()); // factory function var createParent = function _createParent(foo) { // create a object with a Parent prototype return Object.create(Parent, { foo: { value: foo } }); } var Child = (function _Child() { var self = Object.create(Parent); // other stuff return self; }()); var createChild = function _createChild(bar) { return Object.create(Child, { bar: { value: bar } }) }; 

Here's a snippet of code I'm working on that can be used as an example:

 var Sketchpad = (function _SketchPad() { var self = Object.create({}); var mousemove = function _mousemove(e) { this.drawLine(e); }; self._init = function _init() { this.$elem.bind({ "mousemove": mousemove.bind(this), }); this.pens = {}; $("#clear").bind("click", this.clear.bind(this)); $("#undo").bind("click", (function _undoPath() { this.pen.undo(); }).bind(this)); return this; }; self.clear = function() { this.paper.clear(); }; return self; }()); createSketch = function _createSketchPad(id, w, h) { var paper = Raphael(id, w, h); var pen = createPen(paper); var o = Object.create(Sketchpad, { paper: { value: paper }, $elem: { value: $("#" + id) }, pen: { get: function() { return pen; }, set: function(v) { pen = v; } } }); return o._init(); }; 
+77
Jun 14 '11 at 6:23
source share

MooTools is one of the best libraries in terms of OOP Javascript.

You can create classes, interfaces, use inheritance, etc.

Documentation http://mootools.net/docs/core

Tutorial - MooTools OOP http://www.phpeveryday.com/articles/MooTools-Basic-Creating-Classes-MooTools-P919.html

+8
Jun 14 2018-11-11T00: 00Z
source share

You may also be interested in GNU ease.js. If you are not interested in the library itself, its management will go in detail to the implementation details.

You can also see the article article on classic OOP in ECMAScript .

+5
Mar 08 '14 at 19:10
source share

You can try Joose, https://github.com/SamuraiJack/Task-Joose-NodeJS . Although, I personally recommend sticking to the Javascript functionality as provided by ES5.

+3
Jun 14 '11 at 19:07
source share

In the article you referenced, he simply provided an example of what was possible with inheritance in javascript. He did not use the framework, but showing you how to extend your own classes that you wrote.

Framework for javascript include Backbone.js (mvc) and MooTools (oop).

+2
Jun 14 2018-11-11T00:
source share

extjs supports OOP with Ext.define and Ext.extend (and Ext.ns ). See this example at Sencha.com

Ext.extend is an older method, but still useful. You would do something like this:

 Ext.ns('myApp.myPackage'); // create a namespace (function() { // this adds it to the namespace var MyClass = Ext.extend(BaseClass, { property: 1, constructor: function(config) { Ext.apply(this, config); }, method: function(a, b) { this.property = a + b; } }); myApp.myPackage.MyClass = MyClass; }) () 

With Ext.define in Ext 4+ you can do:

 Ext.define('myApp.myPackage.MyClass', // don't need to define the namespace first extend: 'BaseClass' // notice the base class is referenced by a string, requires: 'AnotherClass', mixins: { mixin : 'MixinPackage' }, property: 1, constructor: function(config) { //... } method: function(a, b) { this.property = a + b; } }); 

Note that you can also use traditional OOP in javascript with the 'new' and function.prototype parameters

+1
May 09 '12 at 2:40
source share

If you want to make real powerful OOP in Javascript / Node, you can take a look at Danf 's full-screen open source framework.

It allows you to use OOP (and, therefore, the same classes) on both sides of the server (node) and client (browser). It also provides a good dependency injection mechanism (depending on Symfony2, if you come from the PHP community).

+1
Nov 25 '14 at 16:28
source share



All Articles