-
I'm interested in finding out whether there is ANY difference to declaring a function as:
Code:
function myFunction() {};
or by using a variable
Code:
myFunction = function() {};
I know they both WORK the same, but is using the variable actually adding any extra overhead to your movie? Looking in the Debugger, the results for both look identical.
Any definitive reason why one is better than the other? Thanks.
-
Both are used in with making of classes. But the later method of declaring functions is used to define the Classes functions. Where the first method is used for defining constructors.
Code:
#initclip 1
function MyClass () {
}
MyClass.prototype = new MovieClip();
Object.registerClass("LinkageName",MyClass);
MyClass.prototype.myFunction = function() {};
#endinitclip
So my answer to your question is that there is no difference in regular functions in movieclips, but if you are making Classes you have to use the 2nd method of declaring functions.
Anyone correct me if I am wrong.
-
Well, actually, you can just as easily make a Class with:
Code:
#initclip 1
MyClass = function() {
}
MyClass.prototype = new MovieClip();
Object.registerClass("LinkageName",MyClass);
MyClass.prototype.myFunction = function() {};
#endinitclip
I know, 'cause this is how I usually do it and it works fine. It was just pointed out to me, though, that this might be "bad" coding. Although I disagree with that opinion, I am curious what the difference might be.
Thanks for the reply.
-
I dont know the difference.
For me, it is just like the difference between
removeMovieClip(theClip) and theClip.removeMovieClip();
For thinking process,
function myFunction(){} is trying to define a "function";
myClass.myFunction=function(){} is trying to define a "method";
-
I don't mean to be argumentative (I really don't) and I do appreciate the responses, but again the USAGE for the two is exactly the same. There would be no need to create a function for a single use (that defeats the purpose of a function) and that wouldn't be the only way to use the second type. It's the way I always write my functions, and you can call them anywhere in your movie as:
funcName();
There's nothing temporary about it. In fact, it's the way usually used to assign methods:
MyClass.prototype.myMethod = function() {};
Where the difference will lie (and this is what I'm trying to find out) is in performance or memory, or times where one method would be OBVIOUSLY better than the other. As far as I can tell so far, it seems purely a coding preference, but I want to know for certain.
-
Well, I don't think any function should get changed once it's set. What I'm saying is that whether I use:
function myFunc() {};
or
myFunc = function() {};
I could call EITHER of them later on with:
myFunc();
and there's no difference in how it works. One is simply a function called myFunc, and the other is a variable called myFunc containing an anonymous function.
Now if you are saying "you wouldn't call that again" meaning that you would never SET the method a second time, yes. But you would probably never create a function and then change it later in the movie, be it an object method or not.
-
Code:
myFunction = function(a, b, c){
return a+b+c;
}
myFunction(1,2,3);
You certainly can. All my applications and movies would fall apart if this wasn't true.
-
OK, that makes no sense whatsoever so I'm not sure of your point.
What's my point? I don't think you're understanding my question. There are TWO ways to declare a function. BOTH work equally well. BOTH can be called from anywhere in the movie at any time. NEITHER is temporary. BOTH have the same functionality. Whether the function lies within a function name or in a variable I use in place of a function name, they BOTH do the same job. I happen to choose the variable method as I like to have the function name appear at the beginning of the line (I find it easier to spot when I'm coding and debugging). My question is NOT whether one works or the other, since I know the answer to that question, but rather whether one has been PROVEN a better method and why that might be.
-
Hi tyard,
function myFun(){} - in swf, it is compiled into
function myFun()
--codes--
myFun=function(){} - in swf, it is compiled into
push "myFun"
function()
--codes--
setVariable
I think, there is no much difference in efficiency.
-
I found one instance separate uses are needed.
Code:
function temp1() {
trace("Temp1 Function");
}
this.temp1 = function() {
trace("this.Temp1 Function");
}
temp1();
this.temp1();
/* This Gives errors in code */
function this.temp1() {
trace("this.Temp1 Function");
}
I know there is ways around that last function statement.
Ok, the second difference in uses, this one is all about scope.
Code:
firstFunc();
function firstFunc() {
//Functions that have Scope outside this function
temp1 = function(){trace("2nd")};
this.temp2 = function(){trace("this.temp2")};
//Functions with scope inside this function
var temp = function(){trace("1st")};
function tempA(){trace("tempA")};
//This can not be done, this gives you errors
//function var tempB() {trace("tempB")};
secFunc();
}
function secFunc() {
trace("Entered secFunc");
tempA();
temp();
temp1();
this.temp2();
}
Output:
Entered secFunc
2nd
this.temp2
My conclusion is there are differences when you are doing specific things with scope and functions. Most people and applications dont restrict functions to the scope of one block of code. But there might be a use of such restricting. When the functions are in the scope of the first function that means they will be gone from memory at the end of the first function.
-
Thanks, guys. That helps. So it looks as if, for all intents and purposes, that one is not necessarily "better" than the other.
-
I think that it has to do with inheritance - you can use the second one to load functions from other movies, saving a lot of typing. You don't need to specify the parenthases or content or anything.
_root :
function foo(foo){
trace(foo);
}
button :
onClipEvent(load){
this.FOO = _root.Foo;
}
on(release){
FOO("Some Text Goes Here");
}
You're not explicitly stating the function, but it's implied. If it's the first time that the function is used, then you'd have to put it like this.FOO = function(Foo){.... I guess it would just make it easier if you were inheriting a bunch of functions.
Hope that had some relevance.
-
Hmmm. But in your sample, could you not just as easily use:
Code:
foo = function(foo){
trace(foo);
}
//on mc
onClipEvent(load){
this.FOO = _root.foo;
}
//on button in mc
on(release){
FOO("Some Text Goes Here");
}
which is my point: aren't they pretty much interchangeable?
-
he he
It's really rather amusing reading this thread with one half of a discussion DELETED. Really defeats the purpose of arguing a point and having it there for others to see/learn from/form opinions based on.
-
Danny Gomez Creations ®
WOW, slow down, Einsteins! I have used functions without any MyClass.prototype = new MovieClip(); or
Object.registerClass, and Im very curious about what this kind of programming is good for. Please feed my brain with your knowledge!
-
Functions work fine on their own. You use prototype and registerClass when creating custom Classes in your movies.
Here's a quick example:
Code:
Follower = function() {
this.init();
}
Follower.prototype = new MovieClip();
Follower.prototype.init = function() {
this.rate = Math.random()*40+10;
this.onEnterFrame = this.follow;
}
Follower.prototype.follow = function() {
this._x -= (this._x - _root._xmouse)/this.rate;
this._y -= (this._y - _root._ymouse)/this.rate;
}
Object.registerClass("followerSymbol", Follower);
for (i = 0; i < 20; i++) {
this.attachMovie("followerSymbol", "f" + i, i, {_x:Math.random()*Stage.width, _y:Math.random()*Stage.height})
}
Now create a simple movieclip and export it from your library with the identifier "followerSymbol".
What this code does is create a custom Class which extends the MovieClip object. It does this by setting its prototype property to be a new instance of a MovieClip. now any methods we attach to this Class will be IN ADDITION to all of the MovieClip methods and properties.
We then add two methods, init and follow. Finally, we register a symbol in our library with this new Class. Now when we attach an instance of this symbol to the stage, it will automatically be a new instance of the Follower class.
This, obviously, is a very brief explanation, but hopefully it gives you an idea.
(By the way, notice the constructor I used -- the "variable" way of declaring a function.)
-
tyard
I agree with you that one way is not necessary better than the other. But what about that scoping difference that I posted above?
-
I found that very interesting. The fact that you can use the variable approach to localize a function seems to make that method more versatile (and perhaps gives it the edge?).
-
When the functions are in the scope of the first function that means they will be gone from memory at the end of the first function.
this would mean they are temporary, no?
-
Sigh...
Yes, when declared with var inside a function, like EVERY variable declared with var inside a function, it will be temporary.
This does NOT mean that using:
myFunc = function() {};
OUTSIDE A FUNCTION produces a temporary function that can not be called from anywhere in your movie, which is what you stated before.
Besides, no one else will know what you are talking about with your last post since you DELETED all your previous posts (no doubt the above will be deleted soon as well). If you were so confident in your previous responses (like telling me the code which works without fault couldn't possibly work-- which only showed you never even tried it) then you should have left them there.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|