Jump to content

L8V2L

Recommended Posts

There are several Javascript quizzes out there if you want to go through them.https://www.google.com/search?client=opera&q=javascript+quiz&sourceid=opera&ie=UTF-8&oe=UTF-8If you want to compare yourself, for the first 3 results I get 100% on the w3schools quiz, 9/14 on the Perfection Kills quiz, and 11/20 on the David Shariff quiz.

Link to comment
Share on other sites

Anonymous Functions//An anonymous function is a function that was declared without any named identifier to refer to it. As such, an anonymous function is usually not accessible after its initial creation.//Anonymous function declarationvar anon = function() {    alert('I am anonymous');}; anon();
// I understand how to declare an anonymous function.
// The most common use for anonymous functions are as arguments to other functions, or as a closure.setTimeout(function() {    alert('hello');}, 1000); // Our anonymous function is passed to setTimeout, which will execute the function in 1000 milliseconds.
I kinda(wounding mind a refresher on this) understand the anonymous function being use as an argument.
(function() {    alert('foo');}()); // This is a common method of using an anonymous function as a closure which many javascript frameworks use.
I'm lose... I don't understand this technique their using here.
  //Breakdown of the above anonymous statements://the surrounding braces is a wrapper for the anonymous function//the trailing braces initiates a call to the function and can contain arguments(function(message) {    alert(message);}('foo'));// Another way to write the previous example and get the same result/*An alternative representation of the above places the initiating braces to the surrounding braces and not the function itself, which causes confusion over why the surrounding braces are needed in the first place.*/
What do they mean by wrapper.. and everything else!?!?
(function() {    // ...})();Some have even resorted to giving the trailing braces technique derogatory names, in an effort to encourage people to move them back inside of the surrounding braces to where they initiate the function, instead of the surrounding braces.
Please break it down for me to understand.
Link to comment
Share on other sites

I'm lose... I don't understand this technique their using here.

They are declaring an anonymous function, and then immediately executing it. The main reason to do that is so that the code you want to execute runs in a new scope and won't interfere with or overwrite other variables.This is the function they declare:
function() {    alert('foo');}
Then they put parentheses around the whole thing:
(function() {    alert('foo');})
Then they add the parentheses to execute the function:
(function() {    alert('foo');})();
You'll also see people do that with older jQuery code that uses the $ identifier, which might be conflicting with something else. They will load jQuery and create the jQuery object instead of the $ object, and then pass the jQuery object as a parameter to the anonymous function. Inside the function, the parameter will be called $ so that the code inside the function can use the $ object.
(function($) {  // code here can use the $ object})(jQuery); // pass the jQuery object
Link to comment
Share on other sites

A method is a function that is a member of an object. A property is a value or set of values (in the form of an array or object) that is a member of an object.So this is stating that a method is a function belonging to a object(which I understand stand it's the second part that I need more clarity on) and a property is a value or set of value(in the form of an array or object) what do they mean by the second part?

Link to comment
Share on other sites

A property is like a variable, but it is a member of an object.

I understand that, I'm speak on the array part. What does it mean by array?
Link to comment
Share on other sites

Is this a recursive function:

function work(){  var x = 5, y = 5;  x += y;  return(x);};work();

I have told you how to determine if a function is recursive or not. If it is calling itself then it is recursive.

Link to comment
Share on other sites

I have told you how to determine if a function is recursive or not. If it is calling itself then it is recursive.

.... Is it invoking it self?Show me one of your drawing! Edited by L8V2L
Link to comment
Share on other sites

JavaScript ClosuresRemember self-invoking functions? What does this function do? At least it preserves the counter:Example

var add = (function () {    var counter = 0;    return function (x) {return counter += x}}())add(10);add(15);console.log(add(20));// the counter is now 45
So this is a self invoking function... is it? Edited by L8V2L
Link to comment
Share on other sites

Is this a recursive function:

function work(){  var x = 5, y = 5;  x += y;  return(x);};work();

didn't we already go over this same topic in your other thread?

http://w3schools.invisionzone.com/index.php?showtopic=50075&page=4#entry276748

Link to comment
Share on other sites

function foo(i) {   if (i < 0)      return;   document.writeln('begin:' + i);   foo(i - 1);   document.writeln('end:' + i);}foo(3);begin:3begin:2begin:1begin:0end:0end:1end:2end:3
Stack like behavior it said. How it this so? How can it not be negative for the end:?
Link to comment
Share on other sites

Do you know what a stack is?

A stack is the memory... or reminder for the interpreter to where he left off or and which number, or string, or call back it is on.

Why would it be? What is the first statement in the function?

less than 0. Why do it go back up to three than?I for got to put something in my last message I left. their a saying that I never been told to myself, but what love to do. RTFM or RTFD. Well I WOULD LOVE TO READ THE ######ING MANUEL AND DOCUMENT! JUST GIVE ME THE LINK TO DO SO! Edited by L8V2L
Link to comment
Share on other sites

could you explain to me again of something being copy to a variable and a variable referring to something?

Link to comment
Share on other sites

I think you are spending too much time on oddball cases and "cute" code that has little or no usefulness. Why don't you see if you can write something useful that works?

Link to comment
Share on other sites

Why do it go back up to three than?

Because that's how the stack work. Scopes get added, then removed. The scope with 3 got added first, so it gets removed last.

Well I WOULD LOVE TO READ THE ######ING MANUEL AND DOCUMENT! JUST GIVE ME THE LINK TO DO SO!

I've given you several links to some very good books, but you don't want to read them.
Link to comment
Share on other sites

;// So a cursive function, is a function that calls it self, as so:  function factorial(n) {    if ((n == 0) || (n == 1))       return 1    else {       var result = (n * factorial(n-1) ); // call it self.       return result    } }factorial(1);//1factorial(2);//2//Can you give me more example, and explaining?
Link to comment
Share on other sites

Because that's how the stack work. Scopes get added, then removed. The scope with 3 got added first, so it gets removed last.I've given you several links to some very good books, but you don't want to read them.

Okay... So like the name imply, stack; if it get stack 1, 2, 3 then it must get un-stack; starting from the last one to be put on the stack. 3, 2, 1. Cause, The interpreter need to un-stack them in order, so it can return back to where it left off at.
Link to comment
Share on other sites

I think you are spending too much time on oddball cases and "cute" code that has little or no usefulness. Why don't you see if you can write something useful that works?

I'm studying the syntax structure basic so I can know the components by heart(Cause I love JavaScript). I'm also looking at other people code...(with little to know understand of them) but starting to recognize some to all the components of the code.That's why I ask you to give me code to read, and then ask what is it doing. Edited by L8V2L
Link to comment
Share on other sites

Scalar values are the 3 primitive types listed in the Microsoft article.The word "reference" has a special meaning in programming. In Javascript, some things are references to objects, other things are actual objects. It's not a philosophical difference or opinion, it's what technically happens.

var d = new Date(); // d is a Date objectvar e = d; // e is a reference to an objecte.setDate(10); // e is changed// even though only the e variable was changed, both variables show the same value because e is a reference to dconsole.log(d);console.log(e);var a = 10; // a is set to a scalar valuevar b = a; // the value of a is copied to bb++; // b is changedconsole.log(a); // a stays the same because b is not a reference to aconsole.log(; // b is different
I was wrong about this:Where did that come from? Do you know what object properties are?The computer doesn't care what your opinion is. A function is a kind of object. Like I said, nearly everything in Javascript is an object.Yes, delete only works on object properties, not local variables. When you create a global variable, it is actually a property of the window object, so it can be deleted. Local variables in functions cannot be deleted. Compare what happens when you run these two, one of them creates a global variable which is a property of the window object, and the other creates a local variable inside an anonymous function:
var y = 43;console.log(this);console.log(this.y);console.log(delete y);console.log(y);
(function() {var y = 43;console.log(this);console.log(this.y);console.log(delete y);console.log(y);})();
There's a discussion about the delete operator here:http://perfectionkills.com/understanding-delete/

 

Scalar values are the 3 primitive types listed in the Microsoft article.The word "reference" has a special meaning in programming. In Javascript, some things are references to objects, other things are actual objects. It's not a philosophical difference or opinion, it's what technically happens.

var d = new Date(); // d is a Date objectvar e = d; // e is a reference to an objecte.setDate(10); // e is changed// even though only the e variable was changed, both variables show the same value because e is a reference to dconsole.log(d);console.log(e);var a = 10; // a is set to a scalar valuevar b = a; // the value of a is copied to bb++; // b is changedconsole.log(a); // a stays the same because b is not a reference to aconsole.log(; // b is different
I was wrong about this:Where did that come from? Do you know what object properties are?The computer doesn't care what your opinion is. A function is a kind of object. Like I said, nearly everything in Javascript is an object.Yes, delete only works on object properties, not local variables. When you create a global variable, it is actually a property of the window object, so it can be deleted. Local variables in functions cannot be deleted. Compare what happens when you run these two, one of them creates a global variable which is a property of the window object, and the other creates a local variable inside an anonymous function:
var y = 43;console.log(this);console.log(this.y);console.log(delete y);console.log(y);
(function() {var y = 43;console.log(this);console.log(this.y);console.log(delete y);console.log(y);})();
There's a discussion about the delete operator here:http://perfectionkills.com/understanding-delete/

 

Found it... there isn't anyway to refer to posts that I liked(say the d).
Link to comment
Share on other sites

  • 4 weeks later...

The result is adding an extra 1 to it because I didn't think out the function well. Explaining why might be a bit complicated unless you understand the function. The key to the recursion is this line:return levels + sum(levels - 1);sum() is the function that is currently being executed. First, I'll fix the function to remove the error I had:

function sum(levels) {    if(levels <= 1) {        return levels;    } else {        return levels + sum(levels - 1);    }}console.log( sum(4) );
Now I'll do what's called a "trace". That means I pretend I'm the computer and I follow the instructions
# 8. Call sum() and pass 4 to it# # Inside sum() [level 1]# 1. levels is 4# 2. Is levels less than or equal to 1? It isn't, so go to line 5.# 5. We need to call sum() again, but give it (levels - 1), which is (4 - 1 = 3)#       Store the current value of levels in the stack: stack is [4]# #    Inside sum() [level 2]#    1. levels is 3#    2. Is levels less than or equal to 1? It isn't, so go to line 5.#    5. We need to call sum() again, but give it (levels - 1), which is (3 - 1 = 2)#       Store the current value of levels in the stack: stack is [4, 3]# #       Inside sum() [level 3]#       1. levels is 2#       2. Is levels less than or equal to 1? It isn't, so go to line 5.#       5. We need to call sum() again, but give it (levels - 1), which is (2 - 1 = 1)#          Store the current value of levels in the stack: stack is [4, 3, 2]# #          Inside sum() [level 4]#          1. levels is 1#          2. Is levels less than or equal to 1? Yes, it's 1, so go to line 3.#          3. Return levels, which is 1# #       Inside sum() [level 3]#       5. We have the value of sum()[level 4], it's 1, so add that to levels,#          which is the last element in stack [4, 3, *2*], levels = 2.#          Return (levels + 1) = (2 + 1) which is 3,#          then remove the element from the stack: stack = [4, 3]# #    Inside sum() [level 2]#    5. We have the value of sum()[level 3], it's 3, so add that to levels,#       which is the last element in stack [4, *3*], levels = 3.#       Return (levels + 3) = (3 + 3) which is 6,#       then remove the element from the stack: stack = [4]# # Inside sum() [level 1]# 5. We have the value of sum()[level 2], it's 6, so add that to levels,# which is the last element in stack [4], levels = 4.# Return (levels + 6) = (4 + 6) which is 10,# then remove the element from the stack: stack is empty## 8. Print out the return value of sum(4): 10

 

Hey Ingolme, thanks for this reply post again. You didn't make any mistake(which you already knew.). I probable forgot to take off the number when I enter it in the console to be long out.What tool do traces for me? What type of tools also give time and speed info on each line of code? If you know of any, please tell. Thanks for those post again.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...