Does javascript autobox?

javascript boxing
javascript auto box

While working on another problem, I created this fiddle:

http://jsfiddle.net/tr2by/

function foo() {
    // console.log(_.isBoolean(this));
    console.log(this === true);
}

foo.call(true); // object:[object Boolean]
foo.apply(true); // object:[object Boolean]

Is this an example of auto-boxing?

Going from a value type to a reference type.

Here is a wikipedia def.

First of all I assume you are talking about automatic conversion of primitive values to objects. This happens in two cases in JavaScript:

  1. When you pass a primitive value as the this value to .call or .apply (not in strict mode though).
  2. When you are trying to access a "property" of a primitive value, e.g. "foo bar".split().

In the first case the conversion is permanent, i.e. this will indeed reference an object, in the second the conversion only takes place internally for the duration of the evaluation

If you are not interested in the details of the conversion, you can ignore the rest of the answer.


1. Primitive value as this

When a function is exectued and its this value is not an object, it is converted to one, at least in non-strict mode. This is described in §10.4.3 Entering Function Code [spec] in the ECMAScript 5.1 documentation:

The following steps are performed when control enters the execution context for function code contained in function object F, a caller provided thisArg, and a caller provided argumentsList:

  1. If the function code is strict code, set the ThisBinding to thisArg.
  2. Else if thisArg is null or undefined, set the ThisBinding to the global object.
  3. Else if Type(thisArg) is not Object, set the ThisBinding to ToObject(thisArg). [...]

As you can see in step three the value is converted to an object by calling ToObject [spec].

2. Property access

Something similar happens when you are trying to access properties (§11.2.1 Property Accessors [spec]). The quoted part here explains how the expression foo[bar] is evaluated, i.e. how property access with the bracket notation is evaluated. The part we are interested in applies to dot notation as well.

The production MemberExpression : MemberExpression [ Expression ] is evaluated as follows:

  1. Let baseReference be the result of evaluating MemberExpression.
  2. Let baseValue be GetValue(baseReference). [...]

   8. Return a value of type Reference whose base value is baseValue and whose referenced name is propertyNameString, and whose strict mode flag is strict.

The important step is the last one: No matter to what MemberExpression evaluates, it is converted to a value of type Reference [spec]. This is a datatype only used in the specification and contains additional information about how the actual value should be retrieved from the reference (not to be confused with object references in actual JavaScript code!).

To get the "real" value/result from such a reference, the internal function GetValue(V) (§8.7.1) [spec] is called (just like in step 2 in the above algorithm), where it says:

The following [[Get]] internal method is used by GetValue when V is a property reference with a primitive base value. It is called using base as its this value and with property P as its argument. The following steps are taken:

  1. Let O be ToObject(base). [...]

Example:

Assume we have the expression

var foo = "BAR".toLowerCase();

This is an assignment expression which is evaluated as follows:

The production AssignmentExpression : LeftHandSideExpression = AssignmentExpression is evaluated as follows:

  1. Let lref be the result of evaluating LeftHandSideExpression.
  2. Let rref be the result of evaluating AssignmentExpression.
  3. Let rval be GetValue(rref). [...]

Step 1: The left hand side is evaluated, which is the identifier foo. How exactly identifiers are resolved is not important for this. Step 2: The right hand side is evaluated, i.e. "BAR".toLowerCase(). The internal result of that evaluation will be a reference value, similar to:

REFERENCE = {
    base: "BAR",
    propertyNameString: "toLowerCase",
    strict: false
}

and stored in rref.

Step 3: GetValue(rref) is called. The base of the reference is the value "BAR". Since this is a primitive value, ToObject will be called to convert it to a temporary String object. Furthermore, the reference is actually a property access, so GetValue will eventually call the method toLowerCase on the String object and return the method's result.

Does javascript autobox?, Tagged with javascript, beginners, webdev, tutorial. Autoboxing will connect the primitive to the related built-in prototype object. In our case  Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer , a double to a Double , and so on.

Javascript boxes the this argument provided to call and apply in non-strict mode. From MDN:

if the method is a function in non-strict mode code, null and undefined will be replaced with the global object, and primitive values will be boxed.

Do you know what Autoboxing in JS is? - DEV, In Java, this conversion is called autoboxing. In either language, it allows primitive values to be treated as if they are objects. Literals. JavaScript has literal values  Java Autoboxing and Unboxing - The automatic conversion of primitive data types into its equivalent Wrapper type is known as boxing and opposite operation is known as unboxing. This is the new feature of Java5. So java programmer doesn't need to write the conversion code.

JavaScript as an Object-Oriented Language, What's happening is a process called autoboxing. When you try to call a property or method on certain primitive types, JavaScript will first convert  The how and why of auto-executing functions (in JavaScript) Watch on YouTube: youtu.be/OSZuNlVukDE Call them what you will - auto-executing functions , Immediately Invoked Function Expressions , IIFE , auto anonymous functions , weird-looking function magic - but no matter what you call them they're still confusing .

(Not) Everything in JavaScript is an Object, For instance, JS++ introduces a compare method, which is not available from JavaScript. Thus, the JS++ compiler will auto-box the string value with the System​. To add JavaScript to a Web page, all you have to do is embed JavaScript code in an HTML file. Below the line in which you embed the JavaScript code, you can reference, or call, that JavaScript code in response to an event handler or an HTML link.

Auto-boxing and Unboxing, Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example​  JavaScript has three kind of popup boxes: Alert box, Confirm box, and Prompt box. Alert Box An alert box is often used if you want to make sure information comes through to the user.

Autoboxing and Unboxing (The Java™ Tutorials > Learning the , Everything else is an object. The primitive types boolean, string and number can be wrapped by their object counterparts. These objects are  Many Internet Web sites contain JavaScript, a scripting programming language that runs on the web browser to make specific features on the web page functional. If JavaScript has been disabled within your browser, the content or the functionality of the web page can be limited or unavailable.

Comments
  • Boxing in Javascript is very different from other languages.
  • I'm not sure I'd call that particular behavior "auto-boxing", because the value true is (almost certainly) passed as the boolean primitive itself. The conversion to an object (probably) is done explicitly by the implementation of .call() and .apply().
  • ... but see this section of the spec concerning the semantics of a "Reference". I think that's more like what I'd call "auto-boxing", had I the need to call it anything.
  • Actually the answer that Mr. Kling posted would make my guess wrong; it seems that the coercion to Object type happens differently.
  • Do you know what happens in strict mode then? I guess I could test it myself, but...
  • ... and I guess that in strict mode, that step 2 is different, leaving this set to undefined, right?
  • @Ian In strict mode, we finish setting this at step 1. The this is the actual primitive value.
  • - where is ThisBinding and thisArg defined ... does this even cover the auto-boxing
  • @Pointy: It's an if ... else statement (hard to see, I know). Step 2 is not executed when the condition in step 1 is true.
  • @the_web_situation it depends on what you mean by "auto".
  • @the_web_situation I suppose you could say that -- it's a case of getting a boxed version of a primitive without writing code to perform the conversion explicitly. If you want to call that "autoboxing," go for it, I guess?
  • for...of and spread syntax both use the iteration protocol, which accesses the Symbol.iterator method on the iterable - a property access like on any other value.