Which operator constructs object instances
The next three subsections discuss each of these actions in detail: Declaring a Variable to Refer to an Object Instantiating a Class Initializing an Object Declaring a Variable to Refer to an Object From the Variables section in the previous lesson, you learned that to declare a variable, you write: type name This notifies the compiler that you will use name to refer to data whose type is type.
The Java programming language divides variable types into two main categories: primitive types , and reference types. Variables of primitive types byte, short, int, long, char, float, double, or boolean always hold a primitive value of that same type. Variables of reference types, however, are slightly more complex. Remember, variable declaration alone does not actually create an object. For that, you need to use the new operator, as described in the next section.
A variable in this state, which currently references no object, is said to hold a null reference. If the code in CreateObjectDemo had declared its originOne variable in this manner, it could be illustrated as follows variable name, plus reference pointing to nothing :.
Note: The phrase "instantiating a class" means the same thing as "creating an object"; you can think of the two as being synonymous. Suppose you want to create an object type for cars. You want this type of object to be called Car , and you want it to have properties for make, model, and year. To do this, you would write the following function:. This statement creates myCar and assigns it the specified values for its properties. Then the value of myCar. You can create any number of car objects by calls to new.
For example:. Then you can rewrite the definition of Car to include an owner property that takes a Person object, as follows:.
Instead of passing a literal string or integer value when creating the new objects, the above statements pass the objects rand and ken as the parameters for the owners. So, you can create exactly the Employee — Manager example, but you use slightly different terminology.
First you define the Employee constructor function, specifying the name and dept properties. Next, you define the Manager constructor function, calling the Employee constructor and specifying the reports property. Finally, you assign a new object derived from Employee. Then, when you create a new Manager , it inherits the name and dept properties from the Employee object. In class-based languages, you typically create a class at compile time and then you instantiate instances of the class either at compile time or at run time.
You cannot change the number or the type of properties of a class after you define the class. In JavaScript, however, at run time you can add or remove properties of any object. If you add a property to an object that is used as the prototype for a set of objects, the objects for which it is the prototype also get the new property.
The following table gives a short summary of some of these differences. The rest of this chapter describes the details of using JavaScript constructors and prototypes to create an object hierarchy and compares this to how you would do it in Java.
There are several ways to define appropriate constructor functions to implement the Employee hierarchy. How you choose to define them depends largely on what you want to be able to do in your application. This section shows how to use very simple and comparatively inflexible definitions to demonstrate how to get the inheritance to work. In these definitions, you cannot specify any property values when you create an object. The newly-created object gets the default values, which you can change at a later time.
In a real application, you would probably define constructors that allow you to provide property values at object creation time see More flexible constructors for information. For now, these simple definitions demonstrate how the inheritance occurs. The following Java and JavaScript Employee definitions are similar.
The only difference is that you need to specify the type for each property in Java but not in JavaScript this is due to Java being a strongly typed language while JavaScript is a weakly typed language.
The Manager and WorkerBee definitions show the difference in how to specify the next object higher in the inheritance chain. In JavaScript, you add a prototypical instance as the value of the prototype property of the constructor function, then override the prototype.
You can do so at any time after you define the constructor. In Java, you specify the superclass within the class definition. You cannot change the superclass outside the class definition. An object of these types has properties of all the objects above it in the chain. In addition, these definitions override the inherited value of the dept property with new values specific to these objects.
Using these definitions, you can create instances of these objects that get the default values for their properties. The next figure illustrates using these JavaScript definitions to create new objects and shows the property values for the new objects.
Note: The term instance has a specific technical meaning in class-based languages. In these languages, an instance is an individual instantiation of a class and is fundamentally different from a class. In JavaScript, "instance" does not have this technical meaning because JavaScript does not have this difference between classes and instances.
However, in talking about JavaScript, "instance" can be used informally to mean an object created using a particular constructor function. So, in this example, you could informally say that jane is an instance of Engineer. Similarly, although the terms parent , child , ancestor , and descendant do not have formal meanings in JavaScript; you can use them informally to refer to objects higher or lower in the prototype chain. This section discusses how objects inherit properties from other objects in the prototype chain and what happens when you add a property at run time.
Suppose you create the mark object as a WorkerBee with the following statement:. When JavaScript sees the new operator, it creates a new generic object and implicitly sets the value of the internal property [[Prototype]] to the value of WorkerBee. The internal [[Prototype]] property determines the prototype chain used to return property values.
Once these properties are set, JavaScript returns the new object and the assignment statement sets the variable mark to that object. This process does not explicitly put values in the mark object local values for the properties that mark inherits from the prototype chain.
When you ask for the value of a property, JavaScript first checks to see if the value exists in that object. If it does, that value is returned. If the value is not there locally, JavaScript checks the prototype chain using the internal [[Prototype]] property. If an object in the prototype chain has a value for the property, that value is returned. If no such property is found, JavaScript says the object does not have the property.
In this way, the mark object has the following properties and values:. The mark object is assigned local values for the name and dept properties by the Employee constructor. It is assigned a local value for the projects property by the WorkerBee constructor.
This gives you inheritance of properties and their values in JavaScript. Some subtleties of this process are discussed in Property inheritance revisited. Because these constructors do not let you supply instance-specific values, this information is generic. The property values are the default ones shared by all new objects created from WorkerBee.
You can, of course, change the values of any of these properties. So, you could give specific information for mark as follows:. In JavaScript, you can add properties to any object at run time. You are not constrained to use only the properties provided by the constructor function.
To add a property that is specific to a single object, you assign a value to the object, as follows:. Now, the mark object has a bonus property, but no other WorkerBee has this property. If you add a new property to an object that is being used as the prototype for a constructor function, you add that property to all objects that inherit properties from the prototype. For example, you can add a specialty property to all employees with the following statement:.
As soon as JavaScript executes this statement, the mark object also has the specialty property with the value of "none". The following figure shows the effect of adding this property to the Employee prototype and then overriding it for the Engineer prototype. The constructor functions shown so far do not let you specify property values when you create an instance.
As with Java, you can provide arguments to constructors to initialize property values for instances. The following figure shows one way to do this. The JavaScript logical OR operator evaluates its first argument. If that argument converts to true, the operator returns it. Otherwise, the operator returns the value of the second argument. Therefore, this line of code tests to see if name has a useful value for the name property.
If it does, it sets this.
0コメント