2

--
Hello SO, Today i come before you with a humble question, As I'm obviously missing something fairly basic.

I'm trying, And I can't see why it shouldn't work, To "extend" a function.

To be specific, Consider the following code : It appears that variables have gone missing from the console.log even though they're defined.

However this doesn't seem like the right way to implement what i'm trying to achieve.

The requirement : `Extending a function with variables and methods so that all new instances of that function will receive those variables. What you could consider 'class variables'.

Super = function (){}; //yes it's global.

Super.prototype.alert = function() 
{
    console.log('alert function');  
}

ExtendSuper = function(arguments) //yes it's global
{
  **EDIT , THIS IS THE ANSWER THANKS TO YURY**

 return function () {
    return new Super(arguments);
   }

}

arguments = {} //some object with variables and functions
ExtendedFunction = ExtendSuper(arguments); //yes it's global
success = new ExtendedFunction();//yes, it's global
7
  • 2
    Your code is really messy. You are polluting global scope all the way. First you defining a contructor function. Then suddenly replacing it with an instance of itself. Why? Commented Jun 3, 2014 at 13:52
  • There's only ever a single instance of this 'framework', But it can be fixed. The only 'messy' part is the end, where i just run a few tests. However, The specific thing you commented on is now fixed. Commented Jun 3, 2014 at 13:57
  • What about customView are you sure you want it to be global? Commented Jun 3, 2014 at 14:00
  • I think the question being asked is expecting DomDom.prototype.newthing = something;. Commented Jun 3, 2014 at 14:00
  • 1
    '// The horror, .Stuff and foo are missing, O_O' They are not. console.log calls toString on subClass which returns its source code since subClass is just a function (The one you have assigned to this.View in the very beginning). It doesn't mean your properties gone. They just were not printed. You can use console.dir(subClass) to make sure your props are there. Commented Jun 3, 2014 at 14:06

1 Answer 1

2

EDIT: OP has changed the question in a way making code example irrelevant. Good for him!

You have some weird ideas about inheritance actually. I do recommend you to rethink your application before its too late. :) You do need prototypes because they are essential part of javascript.

Anyway http://jsfiddle.net/uj4ag/

var DomDom = (function(){ //Do not need a function? Use IEFE
    function View(bootstrap) //my "view" class
        {   var view = this;    
            view.$elm = false; view.model = false;
            view.render = function()
            {
                console.log('rendering something');
            }
            $.extend(view,bootstrap);
        };


    return  {
        View:  View,

        extend: {
            View: function(params) {//Let's create a new function :)
                return function() { //it is not an inheritance it is 'currying'
                    return new View(params);    
                }
            }
        }

    }
}());

var SubClass = DomDom.extend.View({ 
        foobar : true,
        alert : function () { alert('hi')},
        render : function() { console.log('rendering something else')},
});

var viewInst = new DomDom.View;
var subClassInst = new SubClass();

viewInst.render();
subClassInst.render();
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry about the Edit! Gotten alot of feedback on the actual code structure (which to me looks the same though), Doing over your answer now :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.