-1

I am new to javascript app development.When i came with functions and all i have found a code like

function Parenizor(value) {
console.log(this.setValue());
}

Parenizor.method('setValue', function () {
console.log('am called');
});

Parenizor.method('getValue', function () {
return 1;
});

Parenizor.method('toString', function () {
return 2;
});

And when i called the function like

var a = new Parenizor(1)

a.setValue()

It throws me error like Undefined not a function..Why is it like this ??..Hope anyone here can find my mistake ..Thanz ..:)

3
  • How about Parenizor.prototype.setValue = function(){code} ?.. Commented Oct 10, 2014 at 17:22
  • @Izzey yeah i know to call via prototype ..but i just wanna call it via .method() ..is it possible like i did or is there any way to call with .method() Commented Oct 10, 2014 at 17:25
  • 1
    Please do not use Crockford as a reference to classical inheritance. None of his code does it correctly and he wants private variables because he's worried about encapsulation but then brakes it by modifying objects he doesn't own (Function and Object prototype) maybe this answer can help you: stackoverflow.com/questions/16063394/… Commented Oct 11, 2014 at 0:41

3 Answers 3

1

It seems your code comes from Classical Inheritance in JavaScript, by Douglas Crockford.

I guess you didn't read this part:

To make the examples above work, I wrote four sugar methods. First, the method method, which adds an instance method to a class.

Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};
Sign up to request clarification or add additional context in comments.

6 Comments

..yeah exactly i got this code from CrickFord blog ..i didnt get what you mean by sugar methods ..can you provide the working by an example
Simply copy-paste the code to the top of your javascript
Just include that code before attempting to use Parenizor.method.
@Oriol i have added the code above but didnt get the result..can you provide the fullcode in ur answer ..it would be really helpful.
@Oriol i have aded the code you given me in the answr ..but it still throws the errir..
|
1

That's not how you define a method. Do it like this:

function K() {}

K.prototype.setValue = function () {};

K.prototype.getValue = function () {};

4 Comments

so calling like Parenizor.method('method') is invalid ??
@user3902466 No, it's not invalid syntax. But it doesn't exist by default, you must define Function.prototype.method manually.
@Oriol yeah ..i got that ..but i have to ask you one thing too ..so why that .method came into action from crockfords blog ??
0

Javascript is OO, not with classes, but with prototypes. First when you declare Parenizor, you're declaring a 'constructor'. You probaly want this:

function Parenizor(){
    var value = null;

    this.setValue = function(val){
        value = val;
    };

    this.getValue = function(){
        return value;
    };

    this.toString = function(){
        return String(value);
    };
}

Or also, setting into the object prototype:

function Parenizor(){}
Parenizor.prototype._value = null;
Parenizor.prototype.setValue = function(){...};
Parenizor.prototype.getValue = function(){...};
Parenizor.prototype.toString = function(){...};

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.