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.

7
  • 2
    What do you want to access? Commented Jun 17, 2014 at 6:24
  • ...oh did you put the question in the string literal? Commented Jun 17, 2014 at 6:25
  • I want to pass 1234 in the method as objMyContent.loaddata(userid).. userid should be from this.userid Commented Jun 17, 2014 at 6:26
  • @cookiemonster.. Yes you're right. Commented Jun 17, 2014 at 6:26
  • objMyContent.toUserID but why pass it in? The loadData method can do this.toUserID Commented Jun 17, 2014 at 6:26

3 Answers 3

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

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

1 Comment

Thanks Kong. Can you provide the same for my code? If possible
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);

3 Comments

Thanks. :) I've used it.
What happened to the prototype? stackoverflow.com/questions/16063394/…
I am sorry, i didnt get you.
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

4 Comments

Whether you use new and the prototype, or that with an object literal and no inheritance, makes absolutely no difference for the question asked.
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.
Really? Nobody uses Crockford's parasitical inheritance today. Where have you seen this?
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.

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.