-
Notifications
You must be signed in to change notification settings - Fork 0
Notes
Sameeri Pavan Kumar Marryboyina edited this page Jul 30, 2015
·
6 revisions
Saibaba Vinayakaya Namaha!
- How does JS express values? Using objects
- Objects are containers for properties.
- Properties are name/value pairs.
- Property that has a Function() object as it's value is a method.
- Object() objects are objects constructed with the Object() constructor function.
- var obj = new Object(); //This is the constructor pattern of creating objects.
- Object, String, Function => Native constructor functions.
- We use the
newkeyword with constructor functions to instantiate objects. - We can create our own custom constructor functions.
- Constructor functions are functions. They are just invoked in a special way. Using the new keyword.
- What produced an object?
- Is it the native Object() constructor function.
- Or is it a special user defined constructor function?
- There are two types of Constructor functions -
Nativethat come with JavaScript language andUser-Defined. - The native constructor functions define the types of the language.
- You create complex objects when you use the constructor function pattern.
- A constructor function creates and returns an object.
- When you invoke a function, the deafult return value is undefined.
- When you invoke a function using the new keyword, the function takes on the role of a constructor function. It creates an empty object, and assigns to the this keyword and returns this.
- Inside the constructor function We can assign values to this and they will be present on the object being returned.
- When we create objects with the new keyword, we are said to "instantiate" that particular kind of object. For example, a Book object, a Task object etc.
- So constructor functions are used to instantiate particular kinds of objects and we can create as many objects as we want. They operate as object factories in this case.
- It's a good practice to Pascal case the constructor function's name.
- What would happen if you created a function that meant to be a constructor function but it was invoked without the new keyword? This is a side-effect and has to be accounted for. The this keyword takes on a different object based on the inclusion of new or not. Depending on where it is defined, the this keyword will attach the properties. If it's a top level function, it will attach to the global object.
- Native constructor functions are Function(), Object(), Array(), String(), Number(), Boolean(), Error(), RegExp(), Date().
- With the native constructors, we have shortcuts, called
literalnotations. - It's preferred to use the literal notations of the native constructors.
- When you create String, Number, Boolean values with the new keyword, the values are complex. Without the new keyword, these constructors return primitive values. They are dual purpose constructors.
- When you use new, you are creating a complex value.
- The literal way of creating strings, numbers, and booleans produces primitive values.
- The typeof keyword can produce
string,number,boolean,function,objectandundefined. - If the object implements
callthen it's a function. Otherwise, it's an object. - Primitive string, boolean and number produces the correct typeof. If created with new, typeof a String() object yields an
object. - http://www.ecma-international.org/ecma-262/5.1/#sec-11.4.3
- The
constructorproperty of an object reveals who constructed the object. - literals conceal the underlying process of using the new operator.
- Primitive values such as 42, 'sameeri', true, false, null, undefined are irreducible. They are the most basic type of values. They are the lowest forms of information available in JS.
- null and undefined are so basic and single valued that they do not need any special constructor functions.
- Primitive values such as strings, numbers, booleans, are equal by value and copied by value.
- Primitive values are wrapped as objects when used like objects. For example, true.toString()