Hobgobjects

I want to know OOP. I basically do, but I want to get the syntax all the way down and also ALSO be able to use them, which I’ve only done to the extent that I’ve copied fabulous OOP luminaries’ code. So far, I don’t have much to say, but it is just! so! important! that I make a note of what I learn when I learn it.

var functionName = function (parameter) {
    this.thing = newThing
}
var objectName = new Object();
objectName.methodName = "methodQuality";
objectName.functionName = functionName;  // this is what I'm having trouble understanding.

Hm. That’s.. that’s not really clear. the this piece of the object creation reminds me a bit of the confusing nature of self in Python. Is that a reasonable analogy?

Going to copy this piece from the codecademy lesson: “Then when we say objectName.functionName = functionName; (line 9), it means whenever we type objectName.functionName( ), this.thing in the functionName method will refer to objectName.methodName.” Maybe I just need to whisper that to myself mystically while sailing along on my bike. Yes, that must be the answer!*

*I’ll get there : )

Advertisement

6 thoughts on “Hobgobjects

  1. Is this from a specific language? It looks like it might be C code, but I’m not sure.

    In my experiences so far, “this” is used specifically for having a class refer to itself. For example, I might have a function being called in my code somewhere that takes “this” as the variable being passed in, like so:

    function(this)

    I’m guessing self in Python is similar?

    I really enjoy reading and commenting on these; they make me think more about what I actually do while programming 🙂

    • Oh jeez, I didn’t say! It’s all JS. I need to tag these handful of posts. I’m so glad you enjoy them!! I keep finding out that people, even those that don’t comment, are reading, and wow, what a great way to stay motivated to keep writing on this, which is THEN an amazing reference for me to come back to when I need, which I’ve already done a handful of life-saving times.

      • Something I was reminded of earlier today in regards to differences between C and C++ that I think you would find interesting:

        In C, it’s not object oriented, so if you want the equivalent of a this pointer you have to build gnarly structs all over the place.

        In C++, since it is object oriented programming, there’s an implicit this pointer for all the variables in the class. Saves so much time!

        We jump between the two frequently as our driver is written in C, and our applications are written in C++, so I’ve gotten to learn just how different the two are, and which I prefer, which is C++ by far.

      • Oh man, a NON object oriented version of C? I guess that was the great improvement in C++! A little mindblowing, that, considering that C++ was(/is) such a significant ancestor to the vast majority of OOP. Very cool – you’ll have to show me some time.

  2. I assume this is javascript, which, as you know, I’m no expert at (disclaimer).

    Yes your analogy of `self in python and `this in JS is totally reasonable. Here is the difference: `this` is actually a keyword in JS, whereas `self` is mere convention in python; you could call that first variable passed into all functions that come under a class anything you wanted. In JS, You do not need to declare `this`, it will just be there to represent the object which you are coding inside of. That being said, you can use them is basically the same way, and just remember that python passes references to objects in the first parameter of class methods, and JS uses the keyword this. I hope this made a least a little sense.

    I don’t know if it’s the code academy explanation, or me not being used to JS, but that explanation is indeed confusing. That looks like a bit of a mess. This has tempted me to look up a bit of JS. I think the quote would be more accurate if it read “…objectName.functionName(“kermit” ), this.thing in the functionName method will refer to objectName.thing, which will have the value “kermit”.”

    Also, for this to work, you would need to fix the function definition to assign `parameter` to `this.thing` (`newThing` is undefined).

    • Cool, thank you. I’ve read your comment a few times now and it does make more sense each time. And yes, I highly recommend checking JS out, as I am enjoying it so so so very much.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s