0

I've struggled and hacked this Javascript into working:

function mmlfunc(name, evalcallback, mmlparts)
{
    this.name = name;
    // ...
}
mmlfunc.prototype.evalFunc = function()
{
    return this.evalcallback(this.args);
};
mmlfunc.prototype.getMML = function()
{
    var mml = this.mmlparts[0];
    // ...
    return mml;
}

// ...

mmlnum = jQuery.extend(true, {},
    new mmlfunc('Value',
        function() { return this.val; },
        [ '<mn>', '</mn>' ]));
mmlnum.getMML = function()
{
    return this.mmlparts[0] + this.val + this.mmlparts[1];
}

// ...
var n1 = jQuery.extend(true, {}, mmlnum),
    n2 = jQuery.extend(true, {}, mmlnum),
    n3 = jQuery.extend(true, {}, mmlnum),
    n4 = jQuery.extend(true, {}, mmlnum);

n1.val = 6;
n2.val = 7;
n3.val = 8;
n4.val = 9;

How do I get new() working on n1-n4 instead of having to use extend()? What else can I do to clean this mess up?

Thanks.

2 Answers 2

1

Make mmlnum call the base constructor and then extend the prototype. Example on jsFiddle.

function mmlnum()
{
    mmlfunc.call(this,
                 "Value",
                 function() { return this.val; },
                 [ '<mn>', '</mn>' ]);
}

jQuery.extend(true, mmlnum.prototype, mmlfunc.prototype);

And then change your vars to

var n1 = new mmlnum(),
    n2 = new mmlnum(),
    n3 = new mmlnum(),
    n4 = new mmlnum();

n1.val = 6;
n2.val = 7;
n3.val = 8;
n4.val = 9;

Using alert(n1.name) will display Value.

Inheritance on MDC.

Sign up to request clarification or add additional context in comments.

1 Comment

Your solution works quite well. I confess to being quite bewildered when it comes to the Javascript object model as compared to C++/C#/Java, but I'll get through it.
1

It isn't so bad to create the mmlnum object and use $.extend for each n-var here. If they aren't used, then setting up your n-vars would have to look like something like this:

var n1 = new mmlfunc('Value',
                     function() { return this.val; },
                     [ '<mn>', '</mn>' ])),
    n2 = new mmlfunc('Value',
                     function() { return this.val; },
                     [ '<mn>', '</mn>' ])),
    n3 = new mmlfunc('Value',
                     function() { return this.val; },
                     [ '<mn>', '</mn>' ])),
    n4 = new mmlfunc('Value',
                     function() { return this.val; },
                     [ '<mn>', '</mn>' ]));
n1.getMML = function() {
                return this.mmlparts[0] + this.val + this.mmlparts[1];
            };
n2.getMML = function() {
                return this.mmlparts[0] + this.val + this.mmlparts[1];
            };
n3.getMML = function() {
                return this.mmlparts[0] + this.val + this.mmlparts[1];
            };
n4.getMML = function() {
                return this.mmlparts[0] + this.val + this.mmlparts[1];
            };

...which is both way less readable and less DRY. Even if there was a lot to clean up before this, I think you should leave the part you quoted as is.

Comments

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.