JavaScript Object-Oriented Essentials note

Source: Internet
Author: User
Tags shallow copy

Just read "JavaScript Object-oriented essentials" this book, in the existing knowledge system has some fresh understanding, record. There are two types of primitive and reference-type javascript: The original type and the reference type. The original types include string, number, Boolean, Null, Undefined, and reference type to save the object, which is essentially a reference to the memory location where the object resides. The assignment of the original type or the argument to the function is actually a copy of the value of the original type, and the reference type is the copy of the reference. If you modify one of the references, other references will also be affected. If a property in an object is also an object, the difference between a deep copy and a shallow copy is introduced when the object is copied.
var a = {"name": "Hello"}; var b =//  "Hello"= "World"//  " World"

The literal form of the regular expression JavaScript allows the literal definition of a regular expression, unlike the RegExp constructor, where the arguments to the constructor involve escaping.
var reg =/\d+/g; // equivalent to var New REGEXP ("\\d+", "G");
For the original encapsulation type String, number, and Boolean have the original package type, when the value of these types is read, the temporary original wrapper type of the object is automatically created and deleted immediately after use. To cite a more bizarre question:
var name = "Nicholas"= "Zakas"//  undefined

The actual execution of this code is as follows:

var name = "Nicholas"; var New  = "Zakas"null; var New  //  undefinednull;

So when it gets, it reads the temporary variables and burns them after use.

An important feature of object-oriented programming for function overloading is function overloading. The functions in JavaScript do not have the concept of prototypes, and they do not even mention overloading.
function showmessage (message) {  Console.log ("Parameter:" + message);    } function ShowMessage () {  console.log ("No Parameter");} ShowMessage (//  "No Parameter"

Using the concept of the original encapsulation type helps to understand the following process:

var New function ("message", "Console.log (\" Parameter: \ "+ message)"new function ("Console.log (\" No Parameter\ ")"showmessage (//  "No Parameter" )

The essence of overloading is the difference of parameter list, which can be done by judging the number and type of parameters, and then implementing different processing procedures.

1) Arguments Object--an object of similar array type, can iterate through, check its length property; Note: Arguments is not an object of array type, array.isarray (arguments) always returns false     The disadvantage of using arguments is that if the code is complex or long, it is easy to omit the processing of the parameters in the code. 2) In view of the drawbacks mentioned above, it is more common to check whether named parameters are undefined, or to check their type through typeof/instanceof, and to inherit JavaScript through the prototype chain, each object has a prototype object as its prototype object. Inherit its properties and methods.the for-in loop iterates through inherited properties, which can be checked by hasOwnProperty for their own properties. Modifying a prototype object causes all object instances to be affected, and the effect is real-time;by means of constructor inheritance, you can implement initialization of object instances that inherit from the same prototype object, but that are different from the construction process.
functionRectangle (length, width) { This. length =length;  This. width =width;} Rectangle.prototype.getArea=function() {  return  This. length * This. Width;} Rectangle.prototype.toString=function() {  return"[Rectangle" + This. length + "X" + This. Width + "]";}functionSquare (size) { This. length =size;  This. width =size;} Square.prototype=object.create (Rectangle.prototype, {constructor: {configurable:true, Enumerable:true, Value:square, writable:true  }}; Square.prototype.toString=function() {  return"[Square" + This. length + "X" + This. Width + "]";}varRect =NewRectangle (5, 10);varSquare =NewSquare (6); Console.log (Rect.getarea ()); // -Console.log (Square.getarea ());// $Console.log (rect.tostring ());//"[Rectangle 5x10]"Console.log (Square.tostring ());//"[Square 6x6]"Console.log (RectinstanceofRectangle);//trueConsole.log (rectinstanceofObject);//trueConsole.log (SquareinstanceofSquare);//trueConsole.log (SquareinstanceofRectangle);//trueConsole.log (SquareinstanceofObject);//true
The properties of the private member JavaScript object are public, and the properties are privatized through closures. 1) module mode The person object is assigned to an immediately executing function that returns an object that contains the Name property and the Getage, Growolder method. Because of the limitation of the function scope of JavaScript, the variables inside the function cannot be accessed by the context outside the function, which guarantees that the age variable can be accessed only within the function. is actually using closures.
varPerson = (function(){  varAge = 25; return{name:"Nicholas", Getage:function() {      returnAge ; }, Growolder:function() { age++; }  };} ()); Console.log (Person.name); //"Nicholas"Console.log (Person.getage ());// -Person.age= 100; Console.log (Person.getage ());// -Person.growolder (); Console.log (Person.getage ());// -
2) Private member of the constructor
functionPerson (name) {varAge = 25;  This. Name =name;  This. Getage =function() {    returnAge ;  };  This. Growolder =function() { age++; };}varperson =NewPerson ("Nicholas"); Console.log (Person.name); //"Nicholas"Console.log (Person.getage ());// -Person.age= 100; Console.log (Person.getage ());// -Person.growolder (); Console.log (Person.getage ());// -
3) In the example of a private member that combines constructors, the Getage and Growolder methods are unique to the object instance, and if all instances are required to share private data, they can be implemented by combining module patterns and constructors.
varPerson = (function(){  varAge = 25; functionRealperson (name) { This. Name =name; } RealPerson.prototype.getAge=function() {    returnAge ; } RealPerson.prototype.growOlder=function() { age++; }  returnRealperson;} ());varNicholas =NewPerson ("Nicholas");varGreg =NewPerson ("Greg"); Console.log (Nicholas.name); //"Nicholas"Console.log (Nicholas.getage ());// -Console.log (greg.name);//"Nicholas"Console.log (Greg.getage ());// -Nicholas.growolder (); Console.log (Nicholas.getage ());// -Console.log (Greg.getage ());// -
Scope-Safe constructors

This problem arises from the easy omission of the new keyword when creating an object instance. Constructors are also functions, so you can change the value of this by calling them directly without the new operator. In non-strict mode, this is forced to point to the global object, and in strict mode, the constructor throws an error. To resolve this problem, you can check it in the constructor.

function Person (name) {  if(Thisinstanceof person ) {    this. name = name;  }   Else {    returnnew person (name);}  }

JavaScript Object-Oriented Essentials note

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.