instantiate an object within another object? #353
Replies: 10 comments
-
Posted at 2015-03-29 by DrAzzy Can you please post all the code? |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-03-29 by @gfwilliams I think it's because you define Worth checking what's actually in the variable 'bot' |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-03-29 by d0773d @drazzy I edited my original post with all the code. It's really messy and eventually I will split my code up into separate files to later include them. @gordon I checked what's in bot by doing:
I receive what looks like an empty object:
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-03-29 by DrAzzy How did I miss that at first glance, yeah it's definitely what Gordon said (maybe something else too).
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-03-29 by d0773d @gordon @drazzy |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-03-30 by @gfwilliams Yes - in JavaScript, the only real difference between executing a method call and a normal function is what So, if you define the variable |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-03-30 by @joakim Just to complicate things, the value of
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this Which is why we have Also worth reading to understand the flexibility of functions/objects: |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-03-30 by @allObjects There is currently a conversation going in that respect - Serial.on() issues - which is actually more a about this, context classes, objects (instances of classes), functions, methods, callbacks, etc. Javascript has a bit a quirky looking way of instantiating an object from a given class: issuing Invoking a capability, function, or behavior of that class of objects is then more suitably called invoking a method. Defining methods (behavior) is done by assigning functions to the prototype of the (state) defining function. To make it more obvious, lets, call the "MyFunction" a "Person", and provide it with a name 'when born' / created / instantiated:
Now we provide a behavior to the Person - class of instances:
Creating susan and mark and ask them to introduce themselves would then go like this - notice the 'politically not so correct all lower case beginning, but convention complying useful distinction of instance vs. class casing:
Note that we had to make the definition only once as a class, but get multiple instances with same state - .name - and behavior - .introducesYourself(). Unfortunately, the way (most) modules are built and used is the way of singleton. Singleton means: there can exists only / exactly (math.) one instance of that class (so not really a class, because what is a class with one member...). Bit what do you do when you have multiple sensors, and the sensor is made available / connectable via a module that is thinking 'in singleton' (only)? Sometimes, you get away by storing the module in a variable and invoke it several times with connect. This works though only when the module is built in a particular way and returning the instance from the constructor function. (Depending how the require("moduleName") is implemented (with or without caching), instead of storing the module reference in a variable, you just repeat the require(...) with no significant performance penalty.) You can instantiate objects within other object instantiations... nothing wrong with that. It just creates dependencies - which you may have anyway. A less intertwined approach would be to create the containing object with null values for the contained objects first, and then assign the object instance to in a separate step, where you compose the complete containing object (or you go very sophisticated and use a micro / pico container supporting IoC / DI ... pico has nothing to do with Espruino Pico except that it is a smaller version of a bigger thing...) In order to access properties - state and behavior - of a contained object, you can call the objects nested - |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-03-31 by d0773d @allObjects Thanks for your explanations and example code. I have such a limited knowledge of Javascript and I am learning tuns about javascript from my project. I wrote the majority of my pseudo code out on a piece of notebook paper at a coffee shop a few months ago. It's amazing how simple I though my code was going to be until I actually sat down and started writing code. I have no idea how individuals such as @gordon do this for a profession. I write code that sorta works, take a break and then I completely forget everything that I wrote or how it works. Eventually, I will have an 'ahha moment' and all this code will make sense. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2015-04-01 by d0773d @joakim Interesting... I remember I asked a question about .bind() in one of my other posts. I will have look more into .bind() .apply() and call() |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2015-03-29 by d0773d
I'm not sure what the terminology is called.... I am trying to instantiate an object within another object.. I am getting these errors:
Code:
Beta Was this translation helpful? Give feedback.
All reactions