Highlighted

Pass "this" into Function

Explorer ,
Jan 06, 2020

Copy link to clipboard

Copied

I have a function:

 

function addAttach(arNum) {	
	// Add letter to place in wheel
	addLetter[LetterNum].Letter.text = ansArray[arNum];
	// Attach letter holder to container movieclip
	this.Letter_Container.addChild(addLetter[LetterNum]);
	// Next letter container to add
	LetterNum++;
}

 

Letter_Container is a movieclip on the main timeline. How do I get "this" to work in the function when it is called?

 

addAttach.apply(this,i);

 

I've searched about "call" and  "apply," but what I've read doesn't tell me how to get "this" into a simple function. I don't want to create "var obj = {..." I just want "this" to work in the above function. Thanks.

Adobe Community Professional
Correct answer by ClayUUID | Adobe Community Professional

Okay kglad's code won't work for you then because it's AS3 instead of JavaScript.

 

You sound like you're kind of hung up on "this" being weird and special. There's nothing magical about "this". You use it like any other variable. You can pass it to functions, assign its value to another variable, yadda yadda.

 

You want your addAttach() function to operate on a certain movieclip? Then just pass it a reference to the movieclip. It doesn't matter whether the reference originates from "this" or somewhere else. The function shouldn't care. Let go of the notion of trying to force a function's "this".

function addAttach(clip, arNum) {	
	// Add letter to place in wheel
	addLetter[LetterNum].Letter.text = ansArray[arNum];
	// Attach letter holder to container movieclip
	clip.Letter_Container.addChild(addLetter[LetterNum]);
	// Next letter container to add
	LetterNum++;
}
addAttach(this, i);

Or, since your function is already accessing (and modifying!) variables in the lexical scope, you could just store your reference there.

var thisClip = this;

function addAttach(arNum) {	
	// Add letter to place in wheel
	addLetter[LetterNum].Letter.text = ansArray[arNum];
	// Attach letter holder to container movieclip
	thisClip.Letter_Container.addChild(addLetter[LetterNum]);
	// Next letter container to add
	LetterNum++;
}
addAttach(i);

Or, if you can absolutely guarantee that Letter_Container will never be anywhere but on the main timeline, you can use the automatically created variable exportRoot, which points to the main timeline.

function addAttach(arNum) {	
	// Add letter to place in wheel
	addLetter[LetterNum].Letter.text = ansArray[arNum];
	// Attach letter holder to container movieclip
	exportRoot.Letter_Container.addChild(addLetter[LetterNum]);
	// Next letter container to add
	LetterNum++;
}
addAttach(i);
TOPICS
Code, How to

Views

181

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Pass "this" into Function

Explorer ,
Jan 06, 2020

Copy link to clipboard

Copied

I have a function:

 

function addAttach(arNum) {	
	// Add letter to place in wheel
	addLetter[LetterNum].Letter.text = ansArray[arNum];
	// Attach letter holder to container movieclip
	this.Letter_Container.addChild(addLetter[LetterNum]);
	// Next letter container to add
	LetterNum++;
}

 

Letter_Container is a movieclip on the main timeline. How do I get "this" to work in the function when it is called?

 

addAttach.apply(this,i);

 

I've searched about "call" and  "apply," but what I've read doesn't tell me how to get "this" into a simple function. I don't want to create "var obj = {..." I just want "this" to work in the above function. Thanks.

Adobe Community Professional
Correct answer by ClayUUID | Adobe Community Professional

Okay kglad's code won't work for you then because it's AS3 instead of JavaScript.

 

You sound like you're kind of hung up on "this" being weird and special. There's nothing magical about "this". You use it like any other variable. You can pass it to functions, assign its value to another variable, yadda yadda.

 

You want your addAttach() function to operate on a certain movieclip? Then just pass it a reference to the movieclip. It doesn't matter whether the reference originates from "this" or somewhere else. The function shouldn't care. Let go of the notion of trying to force a function's "this".

function addAttach(clip, arNum) {	
	// Add letter to place in wheel
	addLetter[LetterNum].Letter.text = ansArray[arNum];
	// Attach letter holder to container movieclip
	clip.Letter_Container.addChild(addLetter[LetterNum]);
	// Next letter container to add
	LetterNum++;
}
addAttach(this, i);

Or, since your function is already accessing (and modifying!) variables in the lexical scope, you could just store your reference there.

var thisClip = this;

function addAttach(arNum) {	
	// Add letter to place in wheel
	addLetter[LetterNum].Letter.text = ansArray[arNum];
	// Attach letter holder to container movieclip
	thisClip.Letter_Container.addChild(addLetter[LetterNum]);
	// Next letter container to add
	LetterNum++;
}
addAttach(i);

Or, if you can absolutely guarantee that Letter_Container will never be anywhere but on the main timeline, you can use the automatically created variable exportRoot, which points to the main timeline.

function addAttach(arNum) {	
	// Add letter to place in wheel
	addLetter[LetterNum].Letter.text = ansArray[arNum];
	// Attach letter holder to container movieclip
	exportRoot.Letter_Container.addChild(addLetter[LetterNum]);
	// Next letter container to add
	LetterNum++;
}
addAttach(i);
TOPICS
Code, How to

Views

182

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jan 06, 2020 0
Adobe Community Professional ,
Jan 06, 2020

Copy link to clipboard

Copied

addAttach(this,3);

 

function addAttach(_this:MovieClip, arNum:int):void{

_this.Letter_etc

}

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 06, 2020 0
Adobe Community Professional ,
Jan 07, 2020

Copy link to clipboard

Copied

Is this an AS3 or a Canvas document?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 07, 2020 0
Explorer ,
Jan 07, 2020

Copy link to clipboard

Copied

This is a canvas document.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 07, 2020 0
Adobe Community Professional ,
Jan 07, 2020

Copy link to clipboard

Copied

Okay kglad's code won't work for you then because it's AS3 instead of JavaScript.

 

You sound like you're kind of hung up on "this" being weird and special. There's nothing magical about "this". You use it like any other variable. You can pass it to functions, assign its value to another variable, yadda yadda.

 

You want your addAttach() function to operate on a certain movieclip? Then just pass it a reference to the movieclip. It doesn't matter whether the reference originates from "this" or somewhere else. The function shouldn't care. Let go of the notion of trying to force a function's "this".

function addAttach(clip, arNum) {	
	// Add letter to place in wheel
	addLetter[LetterNum].Letter.text = ansArray[arNum];
	// Attach letter holder to container movieclip
	clip.Letter_Container.addChild(addLetter[LetterNum]);
	// Next letter container to add
	LetterNum++;
}
addAttach(this, i);

Or, since your function is already accessing (and modifying!) variables in the lexical scope, you could just store your reference there.

var thisClip = this;

function addAttach(arNum) {	
	// Add letter to place in wheel
	addLetter[LetterNum].Letter.text = ansArray[arNum];
	// Attach letter holder to container movieclip
	thisClip.Letter_Container.addChild(addLetter[LetterNum]);
	// Next letter container to add
	LetterNum++;
}
addAttach(i);

Or, if you can absolutely guarantee that Letter_Container will never be anywhere but on the main timeline, you can use the automatically created variable exportRoot, which points to the main timeline.

function addAttach(arNum) {	
	// Add letter to place in wheel
	addLetter[LetterNum].Letter.text = ansArray[arNum];
	// Attach letter holder to container movieclip
	exportRoot.Letter_Container.addChild(addLetter[LetterNum]);
	// Next letter container to add
	LetterNum++;
}
addAttach(i);

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 07, 2020 0