Object constructor acts as a factory method for primitive data types
The Object constructor acts as a factory method and is capable of returning an instance of a primitive wrapper based on the type of value passed into the constructor.
For example:
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var obj = new Object("some text");
document.writeln(obj instanceof String); //true
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var obj = new Object(213);
document.writeln(obj instanceof Number); //true
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var obj = new Object(true);
document.writeln(obj instanceof Boolean); //true
</script>
</head>
<body>
</body>
</html>
Home
JavaScript Book
Language Basics
JavaScript Book
Language Basics
Primitive Wrapper Class:
- Primitive wrapper class
- Object constructor acts as a factory method for primitive data types
- primitive wrapper constructor vs casting function