Categories
discuss

ANDing javascript objects together

I ran across this chunk of code (modified) in our application, and am confused to how it works:

    function someObject()
    {
        this.someProperty = {};
        this.foo = 
        {
            bar:
            {
                baz: function() { return "Huh?" }
            }
        };

        this.getValue = function()
        {
            return (this.someProperty && this.foo.bar && this.foo.bar.baz && this.foo.bar.baz()) || null;
        }
    }

    function test()
    {
        var o = new someObject();
        var val = o.getValue();
        alert(val);
    }

when you call the test() function, the text “Huh?” is alerted. I’m not sure how the result of getValue is returning that, I would’ve thought doing A && B && C && D would have returned true, rather than the value of D.

Answer

That happens because the Boolean Operators in JavaScript can return an operand, and not necessarily a Boolean result, e.g.:

The Logical AND operator (&&), will return the value of the second operand if the first is truthy:

true && "foo"; // "foo"

And it will return the value of the first operand if it is by itself falsy:

NaN && "anything"; // NaN
0 && "anything";   // 0

That’s why in your example "Huh?" is returned, because all the preceding expressions are truthy:

alert("A" && "B" && "C" && "Huh?"); // "Huh?"
alert(true && true && true && "Huh?"); // "Huh?"

The Logical OR operator (||) has a similar behavior, it will return the value of the second operand, if the first one is falsy:

false || "bar"; // "bar"

And it will return the value of the first operand if it is by itself non-falsy:

"foo" || "anything"; // "foo"

This behavior is often used to set default values, for example:

function test (arg1) {
  arg1 = arg1 || "default value";
}

Note: Falsy values are those that coerce to false when used in a boolean context, and they are: null, undefined, NaN, 0, zero-length string, and of course false. Anything else will coerce to true.

Categories
discuss

Switch to BigInteger if necessary

I am reading a text file which contains numbers in the range [1, 10^100]. I am then performing a sequence of arithmetic operations on each number. I would like to use a BigInteger only if the number is out of the int/long range. One approach would be to count how many digits there are in the string and switch to BigInteger if there are too many. Otherwise I’d just use primitive arithmetic as it is faster. Is there a better way?

Is there any reason why Java could not do this automatically i.e. switch to BigInteger if an int was too small? This way we would not have to worry about overflows.

Answer

I suspect the decision to use primitive values for integers and reals (done for performance reasons) made that option not possible. Note that Python and Ruby both do what you ask.

In this case it may be more work to handle the smaller special case than it is worth (you need some custom class to handle the two cases), and you should just use BigInteger.

Categories
discuss

JavaScript and ASP.NET – Cookies with Key / Value Pairs

This question mixes client-side scripting with server-side parsing.

In some cases, I’m writing a cookie to the user’s browser using the document.cookie property. In other cases, I’m writing the same cookie to the user’s browser through the ASP.NET Response object.

When I’m writing the HttpCookie on the server-side, I am using the Values collection (http://msdn.microsoft.com/en-US/library/system.web.httpcookie.values%28v=VS.80%29.aspx) to store key/value pairs in the cookie. I would also like to be able to write key-value pairs to the cookie through JavaScript.

How do I create cookies with Key/Value pairs via JavaScript that ASP.NET can parse?

Thank you!

Answer

Are you able to get the value of the main cookie via javascript.

For example if your main cookie name is UserDetails and the sub elements are FirstName and LastName asp.net should set the UserDetails Cookie value as follows.

FirstName=Jon&LastName=Doe

Thanks,

Categories
discuss

Is ternary operator, if-else or logical OR faster in javascript?

Which method is faster or more responsive in javascript, if-else, the ternary operator or logical OR? Which is advisable to use, for what reasons?

Answer

The speed difference will be negligible – use whichever you find to be more readable. In other words I highly doubt that a bottleneck in your code will be due to using the wrong conditional construct.

Categories
discuss

Does Java have a built-in Antivirus? Is it true?

Does Java have a built-in Antivirus?
One of my friends told me there is in the JVM itself – it’s called the “sandbox”. Is it true?

Answer

Java does have a security-related concept called “sandbox”, but it works very differently from typical anti-virus products. The latter usually try to catch viruses via signatures or code analysis before they are executed.

The Java sandbox on the other hand allows you to run Java code while witholding from it access to system resources that could be used to to bad things, e.g. no access to any files.

However, only Java applets and Java Web Start applications run in a sandbox per default. Regular java applications have full access to your system.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..