0

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 ..:)

user3902466
  • 11
  • 1
  • 5
  • How about `Parenizor.prototype.setValue = function(){code}` ?.. – orhanhenrik Oct 10 '14 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() – user3902466 Oct 10 '14 at 17:25
  • 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: http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711 – HMR Oct 11 '14 at 00:41

3 Answers3

1

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

function K() {}

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

K.prototype.getValue = function () {};
allyourcode
  • 21,871
  • 18
  • 78
  • 106
  • More explanation: http://stackoverflow.com/questions/6918509/this-method-function-vs-obj-prototype-method-function – sma Oct 10 '14 at 17:22
  • so calling like Parenizor.method('method') is invalid ?? – user3902466 Oct 10 '14 at 17:24
  • @user3902466 No, it's not invalid syntax. But it doesn't exist by default, you must define `Function.prototype.method` manually. – Oriol Oct 10 '14 at 17:28
  • @Oriol yeah ..i got that ..but i have to ask you one thing too ..so why that .method came into action from crockfords blog ?? – user3902466 Oct 10 '14 at 17:30
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;
};
Oriol
  • 274,082
  • 63
  • 437
  • 513
  • ..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 – user3902466 Oct 10 '14 at 17:27
  • Simply copy-paste the code to the top of your javascript – orhanhenrik Oct 10 '14 at 17:29
  • Just include that code before attempting to use `Parenizor.method`. – Oriol Oct 10 '14 at 17:29
  • @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. – user3902466 Oct 10 '14 at 17:37
  • @Oriol i have aded the code you given me in the answr ..but it still throws the errir.. – user3902466 Oct 10 '14 at 17:44
  • Breaking encapsulation by modifying objects you don't own and then calling it sugar. Maybe a warning would be appropriate here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain?redirectlocale=en-US&redirectslug=JavaScript%2FGuide%2FInheritance_and_the_prototype_chain#Bad_practice.3A_Extension_of_native_prototypes – HMR Oct 11 '14 at 00:46
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(){...};
Rubens Pinheiro
  • 490
  • 5
  • 11