Object and function summary in JavaScript _javascript tips

Source: Internet
Author: User
Tags throw exception

After learning the traditional object-oriented language, such as Java C + +, transfer JavaScript is a bit inappropriate, especially in JavaScript, the so-called object function (function), sometimes get dizzy. The following conceptual things are simply sorted out to facilitate learning and understanding, with a little Java object-oriented knowledge to illustrate.

Learn a few concepts first:

1. What is the Object in ecmascripe first, there are 5 simple data types in Ecmascripe: undefined,null,boolean,number,string. There is also a complex data type, which is object.

2. Reference types---The concept of classes in Java

Value of reference type---Concept of objects in Java

3. Function----The concept of methods in Java

4.var---Declare an operator of a variable, because JS variable is loosely typed, the so-called loose is can be used to save any data, the variable is used to represent only a placeholder, so the variable also do not have to specify what String, int, direct VAR can be, note that the object can be declared with Var .

5.Object (), What---the construction method of object in Java, the constructor of object in JS.

Object

JS is dynamic to add properties and methods.
Like creating an instance of Object

var obj = new Object();

The variable obj is the object of the new object ().
Next:

var obj = new Object ();
Obj.name = ' Zeng ';
Obj.age =;
Obj.fun = function () {
  console.log (this.name+this.age);
}

But this encapsulation is not very good, so more often use is the object literal notation

var obj = {
  name: ' Zeng ',
  age:12,
  fun:function () {
    console.log (this.name+this.age);
  }
}
Obj.fun (); Print out: Zeng12

Access to Object properties:

1, using the bracket
console.log(obj["name"]);

2, the use of points, recommended in this way
console.log(obj.name );

function

Functions are an important part of any language, functions are called JS, you can also understand that is the Java method, C language functions, are the same. However, in the Declaration and use, JS functions can be very different.

An example of a function:

function Fun () {return
   "example";
}
Console.log (Fun ()); Output: ' Example '

You can also do this:

var fun = function () {return
  "use expression to create functions"
};
Console.log (Fun ()); Output: "Creating functions using Expressions"

In the example above you will notice that the function has no function name, only one variable name fun, and the end of expression is also a semicolon,
You can think of it as an expression that declares a variable, and that's what the other variables say:

var name = "Zeng"; Variable name and semicolon

Here's an example with a parameter:

function Fun () {return
   arguments[0] + arguments[1] + arguments[2];
}
Console.log (Fun ("This is", "one", "example")); Output: "This is an example"

Is it a bit strange that the function I created first has no parameters, and it gives arguments when it is called, and can get parameters and return in the function.

In fact, here is to illustrate a point, JS function does not mind passing in a few parameters, also do not care what type of parameters! The reason is that the function internal receive parameter is stored with an array!

The array is the arguments above, the array of course does not mind you to pass a few parameters come in, there are parameters I add one, no parameters I am null.

We can use this feature as an example:

Overloading of imitation methods--performing different reactions with different number of parameters

function Fun () {
  if (arguments.length = 1) {return
    arguments[0]*10;
  }
  if (arguments.length = = 2) {return
    (arguments[0]+arguments[1]) *10;
  }
  Return
}
Console.log (Fun (11,111)); return 1220!!!!

No overloads:
The above example is a simulation of the overload of the function, why to imitate it, I create several different parameters of the function will soon.

function Fun (NUM1) {return
  "functions with only one parameter"
} function
Fun (num1,num2) {return

  "with two arguments";
}
Console.log (Fun (11)); Output "functions with two parameters"

It is called a function with a parameter, and execution is indeed a function with two parameters. Reason: JS defines two functions of the same name, which only belong to the later defined function!!! So there is no overloading this feature in JS.

The name of the function is the pointer:

var fun = function (num1,num2) {return
  num1+num2;
};

var new_fun = fun;
fun = null; Set function to null

console.log (New_fun (10,20));//output:
Console.log (Fun (10,20));//Throw exception: Fun is not a function

It can be seen that although fun = null; But before this var new_fun = fun; That is, the pointer also points to the New_fun, so the New_fun () function can execute, and the function body does not have null

Functions can also be used as values:

function Add (fun,num) {return
  fun (num);
}
function add_10 (num) {return
  num+10;
}

Console.log (Add (add_10,200)); Output: 210

You can explain this:

function Add (function  ,  parameter  ) {

  return  function (  passed parameter);
}

Arguments.callee of functions
This is an example of a recursive

function Fun (num) {
  if (num<=1) {
    console.log (num);
    return 1;
  } else {
    console.log (num);
    Return num * Arguments.callee (NUM-1);
  }
Console.log (Fun (3)); Output 6

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.