"JS Review notes" 02 Objects and functions

Source: Internet
Author: User

Well, for a very important thing, I haven't written notes for a few days.

About objects:

  • || Can be used to populate default values, such as: Myapp.name | | No
  • && can be used to avoid errors, myapp.nameobj there is a situation that does not exist, then you can use Myapp.nameobj && myApp.NameObj.Name to avoid scripting errors
  • All objects created by object literals are connected to Object.prototype, and when you create an object you can also select another object as its prototype, such as:
    if (typeof object.beget!== ' function ') {    object.create=function(o) {          var f=function() {};        F.prototype=o;         return New F ();    }    
    } var another_stooge=object.create (Stooge);

  • The delete operator can delete an object's properties, such as: Delete Obj.name
  • To reduce the pollution of global variables, a unique global variable object can be created, and all global variables are written in this global variable object.

About functions:

  • The literal prototype that the object literal produces is object.prototype, and the function object is connected to the Function.prototype. (The prototype object itself is connected to Object.prototype)
  • In addition to the formal parameters defined at the time of declaration, each function also receives two additional parameters: this and arguments. The value of this depends on the mode being called.
  • There are four modes of invocation in JS:
    • Method invocation Pattern
      • Method definition: If a function is saved as a property of an object, it is called a method.
      • Called when this is bound to the object.
    • Function call pattern
      • is called as a function, this is bound to be a global object.
    • constructor invocation Pattern
      • If you call with new in front of a function, then secretly creates a new object connected to the function's prototype member
      • And this will bind to the new object.
    • Apply Call pattern
      • Because functions are objects, functions can also have methods, that is, the Apply method
      • Funcobj.apply (Obj,array), the first value obj is assigned to this, the second value is a parameter array, and Funcobj is the function object
  • The number of actual and formal parameters does not match and does not cause an error
  • The arguments array represents an array of arguments that are actually passed in, which is said to be an array, which is actually a number of array objects, and it does not have any arrays of methods.
  • Extended type features: For a chestnut:
  •  //  Here's a way to add a new function to this book  Function.prototype.addmethod=function   ( Funcname,func) { this . prototype[funcname]=    FUNC;  return  this  ;}; //  depending on whether the number is positive or negative    Number.addmethod (' Integer ', function   return  math[this  <0? ' Ceil ': ' Floor '] (this   -10/3). Integer ()); 
  • The benefit of scope is that it can access parameters and variables of external functions (except this and arguments, of course, there are workarounds, such as an external function this or arguments assignment to a variable of an external function)
  • Closure notation:
    • You can use closures to protect the name of the variable, thus creating something like a private variable
    • The so-called closure, simply speaking, is that the function can access the context in which it was created.
      varGetmyname=function(){    varName= ' Troy '; return{GetName:function(){            returnname; }, SetName:function(newName) {name=NewName; }    }}(); //Note here The two parentheses represent that the outermost function is executed directly, so GetName is actually the object inside getmyname.getname ();//The result is TroyGetmyname.setname ("ASD"); Getmyname.getname ();//results for ASD
  • Memory function is actually a kind of optimization of recursion, which is to save the recursive data in the container of the recursive external number group.
  • What module Ah, Curry is actually a closure of an extension, here is not recorded

"JS Review notes" 02 Objects and functions

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.