0

This is the first time I'm going to use OOP concept in Javascript. Previously I worked on JQuery. I've defined a class like

function myContent() {

    this.toUserID = "1234";
    this.loadMainLabel = function(url) {
        alert("url:"+url);
    }
    this.loaddata(userid) {
       alert("loading data");
    }
}

var objMyContent = new myContent();
objMyContent.loadMainLabel("www.google.com");
objMyContent.loaddata("Here I want to access the userID 1234 which I got in the class ");

But, I'm not sure how to access it & whether I'm going in the right way or not. Any ideas would be greatly appreciated.

Nizam Ali
  • 241
  • 2
  • 9
  • 16

3 Answers3

1

A more typical pattern for OO type JS programming is to declare classes via function prototypes:

function MyClass() {
    this.instanceVariable = 10;
    this.doSomething();
}

//Extend a "super class"
MyClass.prototype = Object.create(SuperClass.prototype);

MyClass.prototype.doSomething = function() {
   ...
};

You can then instantiate MyClass via new:

var myObject = new MyClass();

There's a very nice run down here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript

Kong
  • 8,792
  • 15
  • 68
  • 98
0

Try this :

function myContent() {

    this.toUserID = "1234";
    this.loadMainLabel = function(url) {
        alert("url:"+url);
    }
    this.loaddata = function(userid) {//here you can get toUserId just by this.toUserId
       alert("loading data"+ userid);
    }
}

var objMyContent = new myContent();
objMyContent.loadMainLabel("www.google.com");
objMyContent.loaddata(objMyContent.toUserID);
intekhab
  • 1,566
  • 1
  • 13
  • 19
0

Douglas Crockford's way of writing OO classes is:

var MyClass(arg1, arg2) {
 var that = {}, privateMember = arg1;

 that.constructorArg2 = arg2;
 var privateMethod() {

 }
 that.getPrivateMember() {
   return privateMember; // Getters!
 }
 that.method1(methodArg) {
   // do Something
 }
 that.method2() {
  // do something more
  privateMethod(); // Calling a private method
 }

 return that;
}

then you can do:

var myClass = MyClass(1, 2); // CALL WITHOUT NEW KEYWORD!
myClass.method1();
alert(that.constructorArg2);
alert(that.getPrivateMember());

Using this technique, you can define private methods, private members and their getter / setters!

Read this article for class definitions and this one for inheritance

Varun Achar
  • 14,781
  • 7
  • 57
  • 74
  • Whether you use `new` and the prototype, or `that` with an object literal and no inheritance, makes absolutely no difference for the question asked. – Bergi Jun 17 '14 at 06:54
  • It does matter to the OP, since he's doing it for the first time. Better to know all your options. Most libraries that I've seen use this method for declaring classes. – Varun Achar Jun 17 '14 at 07:02
  • Really? Nobody uses Crockford's parasitical inheritance today. Where have you seen this? – Bergi Jun 17 '14 at 07:13
  • Fair warning Varun; Crockford is very opinionated about constructor functions and prototype but I have yet to see anything published by him that does it correctly. He then complains about his faulty code and blames JavaScript for it. JavaScript is designed in this way and I would think it's wiser to learn it than to pre dismiss it because Crockford says so. The only reason I've seen him give to avoid it is either his faulty code or because he doesn't like the syntax. – HMR Jun 17 '14 at 12:58