Note that there are some explanatory texts on larger screens.

plurals
  1. PODifference between creating an object through the prototype (not prototype framework) property vs jquery namespaced objects?
    text
    copied!<p>I just need to know what is the rudimentary difference between creating an object using these two methods, and the effect of using the objects.</p> <pre><code>////////////////////////////////////////// myTestObject.prototype.var1 = null; function myTestObject() { this.var1 = "test me"; } myTestObject.prototype.action = function() { alert("alertme"); }; // to use var a = new myTestObject(); a.action(); ////////////////////////////////////////// ////////////////////////////////////////// (function() { $.myTestObject3 = { var1 : "test me", action : _actionMethod }; function _actionMethod() { alert("alertme3"); } })(); $.myTestObject3.action(); ////////////////////////////////////////// </code></pre> <p>A few updates... the answers to this question lead me to google for different keys words which lead to two interesting articles which really went into a lot of detail about Constructor vs Literal objects in javascript</p> <p><a href="http://net.tutsplus.com/tutorials/javascript-ajax/the-basics-of-object-oriented-javascript/" rel="nofollow">http://net.tutsplus.com/tutorials/javascript-ajax/the-basics-of-object-oriented-javascript/</a></p> <p>Then as a segue into why to use prototype when using function constructors paying particular attention to the section on <strong>Why is Using Prototype Better</strong></p> <p><a href="http://net.tutsplus.com/tutorials/javascript-ajax/prototypes-in-javascript-what-you-need-to-know/?search_index=1" rel="nofollow">http://net.tutsplus.com/tutorials/javascript-ajax/prototypes-in-javascript-what-you-need-to-know/?search_index=1</a></p> <p>So as a follow up, you can use a combination of the two like in the guitar example below as a good practice. If only one copy of an object is needed and a change to it, knowing it affects the whole script is ok, then an object literal is fine. However if many objects need be created using the prototype approach is best so all created objects use a reference to the same function rather than having a copy of the function.</p> <p>Another good article to make better use of namespacing and combining all these approaches <a href="http://javascriptweblog.wordpress.com/2010/12/07/namespacing-in-javascript/" rel="nofollow">http://javascriptweblog.wordpress.com/2010/12/07/namespacing-in-javascript/</a></p> <p><strong>LAST FOLLOW-UP</strong> I hope others find this useful as these topics threw me for a loop for a while.</p> <p>Knowing what to look for now, I found two really good urls that describe many design patterns and how to intermix them in javascript using several examples.</p> <p>This one describes how to use the module pattern in a way that makes use of the prototype notation. This will allow you to compartmentalize code while still making sure that instances are referencing the same objects. <a href="http://briancray.com/posts/javascript-module-pattern/" rel="nofollow">http://briancray.com/posts/javascript-module-pattern/</a></p> <p>This goes through several design patterns and list advatanges and disadvantages <a href="http://addyosmani.com/resources/essentialjsdesignpatterns/book/" rel="nofollow">http://addyosmani.com/resources/essentialjsdesignpatterns/book/</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload