Jump to content

Is An Object Just A Function?


gobbly2100

Recommended Posts

No. An object basically is a variable that in itself hold other variables (properties) and functions that can be executed within the scope of that object (methods). For example, the document object has properties - such as document.location, giving the current URL of the page, and methods - such as document.write(), which writes text to the page.

Link to comment
Share on other sites

I recently watched a video about JavaScript, and in it, they say that a method is essentially a property that has a function as a value. It's like

var myObject = {	myMethod: function() {		//function code	}}

is the same as

var myObject = {	function myMethod() {		//function code	}}

Now, I'm not much of a JavaScripter, so please forgive me for any syntax errors this may have.

Link to comment
Share on other sites

In java script: a function can return an object and a function is a special type of object. It might help to think of 'Object' as a generic term referring to anything in scripting terminology: a DOM/HTML element is an object, in a script block any literal string ('ABC') or number (123) is an object. Objects may be as simple as a string literal ('ABC') or as complex as you can imagine. I would highly recommend becomming familiar with the fundamentals of object oriented programming, it will make you a more effecient coder and save you a lot of frustration. Please forigve the offsite link, but Wikipedia has a fairly succinct explanation of OOP fundamentals.Wikipedia Object Oriented ProgrammingJavascript/ECMAScript is a prototype based oop implementation; The same concepts apply but the implementation is quite different.Wikipedia Prototype Based Programming

Link to comment
Share on other sites

a function is a special type of object
Err...? Functions are not objects, but objects can contain functions as methods.By the way different languages function differently, in JavaScript a string literal is indeed an object but in other languages, such as PHP, it is not.
Link to comment
Share on other sites

In Javascript a function and an object are very similar. Consider this:

function testfunc(){  this.a = 1;  this.b = 2;  this.c = 3;    this.calc = function() {	return this.a + this.b * this.c;  }  return this;}var testobj = testfunc();alert(testobj.calc());var testobj2 = new Object;testobj2.a = 1;testobj2.b = 2;testobj2.c = 3;testobj2.calc = function () { return this.a + this.b * this.c };alert(testobj2.calc());

Link to comment
Share on other sites

Well true I suppose... but that is just JavaScript trying to add OO functionality after creating an OB language...

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...