Need Some Help With These Six Javascript Questions

32 views
Skip to first unread message

linxiao li

unread,
Apr 2, 2015, 3:31:03 AM4/2/15
to phoenixj...@googlegroups.com
Hi Everyone
I've been learning Javascript for a while and encountered several questions that need to be clarified. After Googling, I got this list of six questions that I really need help with. Any answer, link, discussion or similar question posted will be appreciated! Thank you all for your help!

1. What is bitwise operator, and its usage?

2.What is the usage of Singleton Pattern?

3.Array.prototype.forEach(callback, thisArg). What is "thisArg" and its usage?

4.Return false: Why at the end of some functions we want to return false?

5.function declaration as parameter for other functions
I read this part in Javascript Patterns page 60: Their(refers to function declarations) definitions cannot be assigned to variables or properties, or appear in function invocations as parameters But when I run the following code, it works. Looking for explanations. 
function foo(){return foo”};
function bar(func){return func}
console.log(bar(foo))/ / function foo(){...}
console.log(bar(foo())) //“foo"

6.Sum up the numbers in an array(recursion). When I run the code, I got this error massage: Uncaught RangeError: Maximum call stack size exceeded
   Could someone explain to me what is happening behind the scene causing the error?

my anwser:
var sum = function (myArr) {
   if (myArr.length == 0) {
       return null;
   } else if (myArr.length == 1) {
       return myArr[0];
   } else {
       return sum(myArr) + myArr.pop();
   }
}

standard answer:
var array_sum = function(my_array) {
 if (my_array.length === 1) {
   return my_array[0];
 }
 else {
   return my_array.pop() + array_sum(my_array);
 }
};






Alex Kremer

unread,
Apr 2, 2015, 7:07:55 AM4/2/15
to phoenixj...@googlegroups.com
Let me see if I can answer a couple of these.

1) Bitwise operators look pretty powerful, and based off of just some quick reading I can see some interesting applications.  It is not something that I've used (up till this point) in a lot of my code but I can see the uses. 

For example: http://jsfiddle.net/skhjpxg3/  The variable "name" is set as zero at the start.  By using the name ^= 1 the system will flip the variable from 0 1 0 1 0 1 indefinitely.  This allows you to have a very effective toggle with very little overhead on your code.

There are a ton more examples of applications and I think you could build some really interesting server side stuff.

This said as stated in this stackoverflow http://stackoverflow.com/questions/654057/where-would-i-use-a-bitwise-operator-in-javascript one user says that while it can be faster you sacrifice code readability as it becomes much more difficult to understand exactly what is going on in the application in a readable manner.

Testing this concept above against another method to toggle doesn't reveal a *huge* performance gain though:  http://jsperf.com/bitwisetest  - i'd fine the second method in this example more readable.

2) Singleton patterns are mainly effective when you need to have the system only instantiate a class only one time to control some application/global logic. 

Here is an example :http://jsfiddle.net/70eyj0v9/2/  -- The application variable holds an anonymous function which wraps our UserName and UserAccess levels.  Any other function can easily access Application.getInstance().getUserName() without having to first instantiate the class.    **This is a very simple example, you would of course add more logic into this application area**.

Take a look at this example for more detail : https://carldanley.com/js-singleton-pattern/

3) The ThisArg allows you to define the scope that the evaluation function targets. 

http://jsfiddle.net/rwL5d1df/

Understanding the THIS keyword can be a pain in the ass because the scope can sometimes jump back and forth depending on where the context is actually at.  You'd want to use the thisArg for the array so that the actual array processing function can understand what this references.  In the above example the button click shows you what this returns with the thisarg defined and not defined.

4) Sometimes you'd want to return false so that the application can take some kind of action if there was nothing returned from the function (sometimes).  There are also times where you want the default action to stop if nothing should be returned, or that the default action should stop.  For example if you are writing an input box and only want the user to input numbers you could test to make sure the input value is within a range of keydown values and return false if it doesn't.  This will roll up and stop the input from happening. 

5) The pattern that is described on page 60 is a little different than what you wrote.  Here is an example I put together : http://jsfiddle.net/e1wbncf3/1/ (using the code from the book). 

The global scope is foo.  Foo can be called with console.log(foo()) or console.log(foo) (which returns the actual function.)

But we'd expect that the definition of local() with function bar() would return "6" because it is returning the function that we expect to run when local() is accessed.  Instead it gives you back the function bar() and doesn't actually return what we'd think it would.

As shown in the example by clicking the button you can access "6" from local bar, this would allow you to return back specific functions depending on what you need and do it more or less anonymously.  This is a black hole of functionality and feature that allows your code to be more flexible.

Your code is working because we're not attempting to access a function defined in a function (i think)

6) The reason your code isn't working in this example is that you're creating an infinite loop and the .pop() is never actually running.

http://jsfiddle.net/yjzzy9xs/

In this example all I did was move the .pop prior to the reexecution of the function so that the application can properly get rid of the element in the array.

--
You received this message because you are subscribed to the Google Groups "Phoenix Javascript" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phoenixjavascr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Torchlight

unread,
Apr 2, 2015, 9:42:45 AM4/2/15
to Alex Kremer, phoenixj...@googlegroups.com
These are all really great questions.  They will come up in JS programming a lot!

Thanks Ling!

linxiao li

unread,
Apr 3, 2015, 2:29:27 AM4/3/15
to phoenixj...@googlegroups.com
Wow! Thank you a lot for the elaborate answers, I'll take time and read through it line by line. I really appreciate your help! :)
Reply all
Reply to author
Forward
0 new messages