使用Node.js用Javascript进行OOP编程 [英] OOP Programming in Javascript with Node.js

查看:110
本文介绍了使用Node.js用Javascript进行OOP编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上是在用Javascript做一个小游戏,我想实现在

I am actually playing with Javascript doing a small game and I would like to implement what I've found on http://www.crockford.com/javascript/inheritance.html which is something similar to:

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

我找不到用于使这种开发成为可能的库的任何引用.有任何想法吗?否则,我正在寻找一个好的库来帮助我的OOP开发.

I can't find any reference the the library used to make such development possible. Any ideas? Otherwise, I'm looking for a good library that will aid my OOP developments.

谢谢

我正在寻找Node.js的OOP解决方案/库.请注意,我是Node.js的新手

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

推荐答案

2个月后

也许您确实需要一个库,但ES5太复杂了,因此我创建了 pd

我正在寻找Node.js的OOP解决方案/库.

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

不需要.您有 ES5 .

JavaScript没有经典的OOP.它具有原型OOP.

JavaScript does not have classical OOP. It has prototyping OOP.

这意味着您只有对象.您只能对对象进行扩展,操作和克隆.

This means you have only objects. The only thing you can do with objects is extend, manipulate and clone them.

操作

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

扩展

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

克隆

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

克隆并扩展

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

重要的是要了解 Object.create Object.defineProperties 工作.

It's important to understand how Object.create, Object.defineProperty and Object.defineProperties work.

克隆操作实际上不是克隆.它正在根据蓝图创建一个新对象.蓝图是一个对象.它将蓝图放置在[[Prototype]]中. [[Prototype]]位于.__proto__属性中,我将在该属性中进行演示.

The cloning operation isn't actually cloning. It's creating a new object from a blueprint. A blueprint is an object. It places the blueprint in the [[Prototype]]. The [[Prototype]] lives in the .__proto__ property which I'll 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;

免责声明: .__proto__已弃用.不要在代码中使用它.它具有用于调试和健全性检查的功能.

Disclaimer: .__proto__ is deprecated. Don't use it in code. It has it's uses for debugging and sanity checks though.

这里的要点是,从r中的o访问属性必须将原型链向上移动3个级别,这会变得昂贵.为了解决该问题,而不是克隆随机对象,您应该克隆特定的蓝图(每个对象应该有一个蓝图).

The main point here is that accessing properties from o in r it has to walk 3 levels up the prototype chain and this gets expensive. To solve that problem, rather then cloning random objects you should clone specific blueprints (and you should have one blueprint per 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 from some code I'm working on that you can use 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();
};

这篇关于使用Node.js用Javascript进行OOP编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆