• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Pass "this" into Function

Explorer ,
Jan 06, 2020 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.

TOPICS
Code , How to

Views

332

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
community guidelines

correct answers 1 Correct answer

LEGEND , Jan 07, 2020 Jan 07, 2020

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 so

...

Votes

Translate

Translate
Community Expert ,
Jan 06, 2020 Jan 06, 2020

Copy link to clipboard

Copied

addAttach(this,3);

 

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

_this.Letter_etc

}

 

Votes

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
community guidelines
LEGEND ,
Jan 07, 2020 Jan 07, 2020

Copy link to clipboard

Copied

Is this an AS3 or a Canvas document?

Votes

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
community guidelines
Explorer ,
Jan 07, 2020 Jan 07, 2020

Copy link to clipboard

Copied

This is a canvas document.

Votes

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
community guidelines
LEGEND ,
Jan 07, 2020 Jan 07, 2020

Copy link to clipboard

Copied

LATEST

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);

Votes

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
community guidelines