We'll go into detail on these statements later in this subsection; for now, here's a brief description.
Question:
We'll go into detail on these statements later in this subsection; for now, here's a brief description. The first line creates a variable named o1 and initializes it with a value of type Object by calling on the built-in constructor Object() using a JavaScript new expression. The second line then adds a property named testing to the o1 object and assigns a String value to this property. And the third line then deletes this property from o1. Dynamic property creation is yet another example of JavaScript's flexibility, but also of its lack of certain safety features: if you misspell a property name in an assignment statement, you won't get an error message, but instead you'll create a new property. This is something to watch for when you're debugging JavaScript code. Now we'll cover object creation and dynamic creation and removal of properties in somewhat more detail. First, as shown in the example, expressions beginning with the keyword new are used to create objects. Syntactically, a new expression begins with the new keyword followed by an identifier corresponding to an object constructor followed by a parenthesized list of zero of more arguments. We'll learn how to create user-defined constructors later in this chapter. A new expression causes a new empty object to be created and then calls the specified constructor, supplying it with this new object plus the specified argument values. The constructor can then perform initialization on the object, which might involve creating and initializing properties, adding methods to the object, and adding the object to an inheritance hierarchy from which it can inherit additional properties and methods. In the case of the Object() constructor, no properties or methods are added directly to the new object by the constructor, but the object is modified so that it inherits several generic methods including default toString and value Of methods used when converting the object to String and Number values, respectively. The values produced by these default methods are not particularly useful, but they at least prevent a run-time error in case an attempt is made to apply data type conversion to the object.
We next turn to property creation. As we saw, when a JavaScript statement attempts to assign a value to an object property, and the property does not exist in the object, then a property with the given name is created in the object and assigned the specified value. This happens even if the object has inherited a property with the same name, since an inherited property or method actually resides in a different object. Finally, as shown, the keyword delete can be used to remove a property from an object. Syntactically, a delete expression begins with the delete unary operator followed by a reference to an object property, such as o.testing. You should not attempt to delete a nonexistent property (technically, this should be legal, but some browsers will throw an exception.