3

Sometimes in JavaScript I need many constructors and objects for pseudo-classes because I like objective very much so I do something like:

var anyClass = (function (settings) {
    var staticPrivate = {}, staticPublic = function () {
        var public = this, private = {};
        (function constructor (here, the, args) {
            this.hta = [here, the, args, arguments];
        }).apply(this, arguments);
        arguments = undefined;delete arguments;
        private.stuff = function () {}
        Object.defineProperties(public, {
            "e.g. length": {
                get: function () {
                    return private.length;
                }, 
                set: function (newValue) {
                    return;
                }, 
                enumerable: false
            }
        });
    };
    Object.defineProperties(staticPublic, {
        "staticFinalHiddenString": {
            get: function () {
                return "YEAH, I'm static and final and hidden...";
            }, 
            set: function (newValue) {
                return "You cannot set me.. :P";
            }, 
            enumerable: false
        }
    });
    staticPrivate.some = function (init) {
        if (settings.some == "settings") init();
    }
    window.requestAnimationFrame(function () {
        staticPrivate.some(function (I) {
            run(on, first, render);
        });
    });
    return staticPublic;
})({
    some: "settings", 
    here: null
});

And that every time, so now I want a constructor that creates a new class for me. I think on this:

new Class({
    constructor: function (here) {
        is(my + constructor);
    }, 
    properties: {
        name: {
            getter: function () {}, 
            setter: function (newValue) {}, 
            hidden: false, 
            static: false, 
            final: false
        }, 
        version: {
            getter: function () {
                return 0.3;
            }, 
            setter: function (newValue) {}, 
            hidden: true, 
            static: true, 
            final: true
        }
    }
});

but my problem is that I have no idea how to create an prototype/constructor with an constructor class.prototype.prototype does not work.

I just tried that:

var Class = (function () {
    var Class = (function () {
        var constructor = function () {
            return (function (information) {
                this.prototype = {};
                var properties = {};
                for(var key in information.properties) {
                    properties[key] = {
                        get: information.properties[key].getter, 
                        set: information.properties[key].setter, 
                        enumerable: !information.properties[key].hidden || true
                    };
                };
                Object.defineProperties(this.prototype, properties);
                return this;
            }).apply(this, arguments);
        };
        return constructor;
    })();
    return Class;
})();

That does not work for me :C

I hope you can help me. Thanks...

loresIpsos
  • 327
  • 1
  • 2
  • 12
  • 4
    Javascript is not an object-oriented language. Everytime I used (or abused) Javascript as Object-Oriented language I ended up in a huge mess. – Mathias Vonende May 18 '15 at 13:56
  • @maze-le see [Introduction to Object-Oriented JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript) – Aprillion May 18 '15 at 14:16
  • 6
    @maze-le: Javascript is an object oriented language. It's just not a class based language. – slebetman May 18 '15 at 14:17
  • I take it you've read this: [How does JavaScript .prototype work?](http://stackoverflow.com/questions/572897/how-does-javascript-prototype-work) – Liam May 18 '15 at 14:18

1 Answers1

0

I understood my mistake and now I can return somthing when I have an constructor.

The var Class = function () {}; and the Class.prototype in the closure function with .apply(this, arguments) does the thing, that is why I can return Class in the constructor function, if I would just do

var Class = function () {
    var ClassICreate = function () {};
    ...
    return ClassICreat;
}

it would not work, because you cannot return from a constructor, because it is an object.


That is how it works for my:

var Class = (function () {
    var Class = function () {
        return (function (information) {
            var Class = function () {};
            var properties = {};
            for(var key in information.properties) {
                properties[key] = {
                    get: information.properties[key].getter, 
                    set: information.properties[key].setter, 
                    enumerable: !information.properties[key].hidden || true
                };
            };
            Object.defineProperties(Class.prototype, properties);
            return Class;
        }).apply(this, arguments);
    };
    return Class;
})();

After I found the answer it looked so easy to me, and thanks for the comments, they helped my to find the right answer...

loresIpsos
  • 327
  • 1
  • 2
  • 12