Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Noob question re "new" and "this" 2

Status
Not open for further replies.

doctorChuckles

Programmer
Feb 2, 2007
20
US
Hi,

According to the Dummies book, "this" and "new" are operators. However, they don't show up in lists of operators, so they must be a special class of operators.

The explanation in the Dummies book is incomprehensible, and I've found other incomprehensible explanations elsewhere. I'm not getting what "new" and "this" do. Yet it must be very simple. I understand the syntax is this [.whatever]

Can someone either explain very simply? Or tell me if my guess is about right?

If my guess is right, "this" probably means something like "put" in an xtalk language. I.e.,

get somethingOrOther
put sometingOrOther into var2

so maybe "put sometingOrOther into var2" would go
this[ .var2]

That's only a guess, though, and I can't guess about "new"

Please clue me just a little.

Thanks,


Tim
 
this refers to the object that the method (function) or property (variable) belongs to. For instance:
Code:
<form action="" method="">
<input type="text" onclick="this.value=''";>
</form>

means that whatever object the onclick event handler is part of will have it's value property set to a zero-length string. It refers to the object without having to specifically provide a name or id for the object. The "parent" of that particular onclick is the text input, and that's what the this refers to.

I've never seen the dot operator used inside square brackets like your example shows. The square brackets indicate an array element index of some kind, though can be used to access an objects functions and properties as an associative array, which is another, more complex lesson itself.

new is a way to create new instances of objects. You can use new to create objects like Number() or String(), but most often the operator used to create a more complex object, like an Array(), Image(), or a user-defined object.

Lee
 
Just to touch on Lee's last point re user defined objects... a bit of fun code to play around with:
Code:
// In Javascript, user-created "classes" are functions.
function myNewObject(){
	// We refer to member variables of the class using the
	// 'this' operator.
 	this.message = "Hello World";
}

// We can create member functions by defining the function
function myNewObject_setMessage(strMessage){
	this.message = strMessage;
}
// And adding it to the class using the prototype object
myNewObject.prototype.setMessage = myNewObject_setMessage;

// Having written a class we can instantiate it using the 'new'
// operator and read and write information to it using the member
// variables and functions we created.
function createNewObject(){
	// instantiate a new instance of the 'myNewObject' class using the 
	// 'new' keyword operator.
	var newObject = new myNewObject();
	
	// We can then use the default value stored in it's "message" variable
	var strMessage = "The current message value of the new object is: " + 
					newObject.message + ".\n\nPlease specify a new message.";
	var strNewMsg = prompt(strMessage, newObject.message);
	
	// And change the value in there using the member functions
	newObject.setMessage(strNewMsg);
	// Note that:
	// newObject.value = strNewMsg;
	// would also be valid here.
	
	// we can see that running the member function changes the value stored
	// in the member function.
	alert("The message value of the object is now: " + newObject.message);
}

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]

Enable Apps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top