JavaScript function section

Source: Internet
Author: User

object:JavaScript's object literal notationAllows you to create objects by simply listing the parts of an object. This is also a source of inspiration for JSON. The so-called object literal is a group of key-value pairs that are contained in {}.
var stooge={' first_name ': ' Jack ' ,' last_name ': ' ma '};
The value within the object can be stooge[' first_name ' or stooge.first_name. It is illegal to take a value from a undefined object, such as a stooge.name.me, and the TypeError error is reported at this time.     The workaround is to use &&,stooge.name&&stooge.name.me, which returns undefined.     Change the value within the object with Stooge.first_name= ' mark '; if First_Name does not exist, a new attribute is added to the object. Traverse the object with for or for in.
 for (var in stooge) {alert (stooge.name);}
The disadvantage of for-in is that the properties in the traversal object are not sequential, and for sequential output you can use the for
var names=[' first_name ', ' last_name '];  for (var i=0;i<names.length;i++) {alert (stooge.names[i]);}
Delete the properties in the object by deleting them. Delete Stooge.first_name Prototypes:Each object is connected to a prototype object and can inherit properties from it. objects created by literal literals are connected to the Object.prototype. The prototype connection is only useful when retrieving (this is helpful, we need to know that the change does not change the value in the prototype), if you retrieve the property of an object, the object does not retrieve the prototype object it points to, until the final Object.prototype, and then returns undefined. function:The function is the object, the end of the prototype chain of the object is Object.prototype, and the end of the function's prototype chain is Function.prototype. the creation of the function:Each function is created with two properties attached, a function's context, and a function implementation code (the Code for the function implementation is also called the "call" property, which can be considered a call property to call the function when invoking a function).
var add=function(var a,var  b) {return a +b;};
The above is a function created with literal literals. Functions created by literals include a connection to the context (called a closure). call to function:The called function also receives the this and arguments parameters in addition to the arguments that are declared.     The value of this depends on the mode of invocation, with a total of four invocation patterns: Method invocation, Function call, constructor call, apply call. Method invocation:
var myobject={var value=0; add:function(i) {//  Here the function is declared as the method returnthis. value+i; // this is MyObject. }};myobject.add (2); // This is the method invocation of the function. The this in the method call function is the owning object itself. 
function Call: (easy wrong point here)To illustrate this point, take the following instructions, and then look at an example. It is important to note that this refers to the global windows at the time of the function call, The direct effect of this problem is when the function's internal function is called
var i=100; var myobject={    i:ten, out    :function  (value) {        var  function  () {            this. i);        }        Infun (); // here is the function call     }}myobject.out (10); // here is the property (method) Call
In the example above, Infun () is called as a function call, this refers to Windows, so this.i=100. The final result is 110. In order to solve the above problem, you can use the following method:
var i=100; var myobject={    i:ten, out    :function  (value) {        var that=  this; // This is where the method call is bound to the that property, and the problem does not occur when you use it below.        var function () {            +that.i);        }        Infun ();    }} Myobject.out (10);
Constructor call: I do not like this way of the function call, for I am already familiar with Java, I think it is very strange, originally the function assigned to the variable is strange. Here is a brief talk.
varmyob=function(ss) {varSTA;  This. sta=SS;} MyOb.prototype.fun=function () {    return( This. STA);};varoo1=NewMyOb (' oo1 ');//This is the key to calling the MYOB function at new, which creates an object in the back that is assigned to OO1,MYOB this is the newly created object, which is the oo1varOo2=NewMyOb (' Oo2 '); alert (Oo1.fun ());//return oo1, because each STA is saved in the corresponding objectAlert (Oo2.fun ());//back to Oo2
Apply Call : This call allows us to pass this manually.
var myob=function() {    return (this. sta);   Here is the app}var app={    sta:"I am ok!" }var res = myob.apply (APP); // manually assign the app to thisalert (res) here;
The call to the function here is not complete, because it says that there is a arguments parameter in addition to this. The line surface will see what this parameter does.
var function () {    var i=0;     var sum=0;      for (i;i<arguments.length;i++) {        sum+ =arguments[i]    ;    } return sum;} Alert (Fun (1,2,3,4,5));
As seen above, it is an array containing the passed arguments (the underlying is just an array of classes).

JavaScript function section

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.