Jump to content

Constructor Functions(Objects)


jimfog
 Share

Recommended Posts

Take a look at the following code. The author claims here(How to develop a firefox extension that he has created an object(scroll down till you find the segment that is referring to this javascript code).From what i know the only way you can create objects in JAvascript is through constructor functions and with the "new" keyword.DO you see any "new" keyword in this code?Can somebody explain it to me please.

var linkTargetFinder = function (){var prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);	return	//here, the value of the first function is returned to the following function 	{init : function () {gBrowser.addEventListener("load", function () {				var autoRun = prefManager.getBoolPref("extensions.linktargetfinder.autorun");				if (autoRun) {linkTargetFinder.run();}}, false);},      		run : function () {			var head = content.document.getElementsByTagName("head")[0],				style = content.document.getElementById("link-target-finder-style"),				allLinks = content.document.getElementsByTagName("a"),				foundLinks = 0;			if (!style) {				style = content.document.createElement("link");				style.id = "link-target-finder-style";				style.type = "text/css";				style.rel = "stylesheet";				style.href = "chrome://linktargetfinder/skin/skin.css";				head.appendChild(style);			}				for (var i=0, il=allLinks.length; i<il; i++) {				elm = allLinks[i];				if (elm.getAttribute("target")) {					elm.className += ((elm.className.length > 0)? " " : "") + "link-target-finder-selected";					foundLinks++;				}			}			if (foundLinks === 0) {				alert("No links found with a target attribute");			}			else {				alert("Found " + foundLinks + " links with a target attribute");			}		}	};}();window.addEventListener("load", linkTargetFinder.init, false);

Edited by jimfog
Link to comment
Share on other sites

You typically use special constructor function if you want to create more than one copy of an object. That's where you use the new keyword.The example you posted is a self-executing function that returns one copy of an object, assigned to linkTargetFinder. You can tell it's self-executing by the () at the end of the function definition. Because it is an anonymous function, it cannot be executed in any other way (it has no name).

Link to comment
Share on other sites

He is creating an object using "object literal" syntax or JSON (JavaScript Object Notation) syntax.var obj = {prop: "property", func: function() { alert("this is a function"); }};Absolutely everything in JavaScript is an object, including functions, strings, numbers, events, etc.

Link to comment
Share on other sites

And using the new keyword:

function MyObject() {   this.prop = "property";   this.func = function() {	  alert("this is a function");   }}var obj = new MyObject();

As you can see there are a lot of ways to create objects. :)

Link to comment
Share on other sites

So, let clarify things a little:1. There are more than one ways to make in object in Javascript-one of them is the object literal notation which is the code in the example i attached.2. Regarding the object literal, i thought that the syntax is sth like that: var john={properties and methods here}but now you are telling me that besides the above there can be an object literal with a code like this: var john= function(){properties and methods here}Am i saying it correctly?Thanks for helping me understand this-sometimes the code to a newbie like me seems confusing.

Link to comment
Share on other sites

Shadow's Post #3 shows how to create an object literal.My Post #4 shows how a self-executing function can create an object literal and return it. It could just as easily have assigned the object literal to a variable and then returned the variable, which is the way I would usually do it.

Link to comment
Share on other sites

1. yes.2. yes.The object literal is this:var obj = function () { return { prop: "property", func: function() { alert("this is a function"); } };}();The function is a self executing function which returns the object literal.

Link to comment
Share on other sites

1. yes.2. yes.The object literal is this:var obj = function () { return { prop: "property", func: function() { alert("this is a function"); } };}();The function is a self executing function which returns the object literal.
Is the return keyword and the self invocation feature necessary for the object literal to work?
Link to comment
Share on other sites

Is the return keyword and the self invocation feature necessary for the object literal to work?
No. It's just a different way to create an object. I can't really speak on benefits/downfalls to one over the other though. I haven't used/don't understand them quite enough to do that yet. Edited by ShadowMage
Link to comment
Share on other sites

no. return is just to return something from a function and is often used. self-invocation is typically used in conjunction with anonymous functions. The example shown is using both, but not necessarily because you have to. an object literal can just be this, too:

var me = {  "screenname" : "thescientist",  "posts"		   :  4000}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...