0

Basically Dub and Dub.socialize objects already exist as an included library. I'm trying to extend the library with some additional custom functions that I created.

I attempted the following concept below:

Dub.socialize = {
    setUID : function(preUID, postUID)
    {
        // .. function code here
    }
}

However, I receive the following error "Uncaught TypeError: Cannot set property 'setUID' of undefined" from my console.

Obviously my knowledge of objects is a bit misled. What would be the proper method of extending this function into the already existing object library?

4
  • which line causing the issue ? Commented Mar 5, 2014 at 16:35
  • That error doesn't really seem to fit with an object literal like you have in the snippet. Are you attempting to reassign setUID? Dub.socialize.setUID = ...;? Commented Mar 5, 2014 at 16:38
  • If Dub.socialize already exists, you can do Dub.socialize.setUID = function ()... Commented Mar 5, 2014 at 16:43
  • 1
    @NikolaDimitroff - if Dub.socialize already exists, he must do it the way you showed, with his code he loses the existing object with all its existing methods/properties. Commented Mar 5, 2014 at 17:04

3 Answers 3

1

A simple solution could be

Dub.socialize.setUID =  function(preUID, postUID) {};
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

Dub.socialize.prototype.setUID = function(preUID, postUID) {
    ...
};

Object Constructor and prototyping

Edit: Realized you're working with a "static" object. This only works for something that is instantiated, and since you're not making new instances, this doesn't apply.

2 Comments

Dissdent, I figured it was indeed in relation to "prototyping", however it doesn't seem to fit properly. One should still receive the error "Uncaught TypeError: Cannot set property 'setUID' of undefined".
Yeah, I realized that the reason this didn't work is because prototyping is for creating instance objects (e.g. new Dub.socialize()) rather than attaching to a "static" object.
0

If you are going to create the function for declared object means then you have to use "prototype" keyword for example.

`var Dub = { socialize: new Object() };

Dub.socialize.prototype.setUID = function(preUID, postUID) { // Function Body };`

http://www.javascriptkit.com/javatutors/proto3.shtml

Comments

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.