JS data type (original type and reference type) thought by JS Apply and call method

Source: Internet
Author: User

Original address: JS data type (original type and reference type) thought by JS Apply and call method

The difference between the call method of JS and the Apply method is that the second parameter is different, they all have 2 parameters, the first is an object (that is, you need to inherit B with object A, then the first argument is a, not null), the call method is a list of the second parameter, can be

Obj.call (null, 1,2,3,4);

  

Instead, apply the second parameter to an array. That's the difference, and here's how they're known.

The most common use of apply is to find the maximum and minimum values in an array, and you can combine 2 numbers and:

var max=math.max.apply (Null,array);  var min=math.min.apply (Null,array); var arr1=new Array ("1", "2", "3"); var arr2=new Array ("4", "5", "6"); Array.prototype.push.apply (ARR1,ARR2);//Add an entry to the end of the array and update length to return the array length.

  

Here the Array.prototype.push itself cannot merge 2 arrays, as follows:

var arr1 = [' 1 ', ' 2 ', ' 3 ']; var arr2 = [' 4 ', ' 5 ', ' 6 ']; Alert (Array.prototype.push (ARR1,ARR2));//returns 2 alert (Array.prototype.push.apply (ARR1,ARR2));//Return 6

  

Questions:

var arr1 = [' 1 ', ' 2 ', ' 3 ']; var arr2 = [' 4 ', ' 5 ', ' 6 ']; Arr1.push (ARR2); alert (arr1);//return is 1,2,3,4,5,6? Puzzled

  

Of course, apply and call can also be used to inherit the methods of the class.

For a simple example:

Four ways to create objects:

First Kind

function people (name, sex) {this.name = name; this.sex = sex; this.show = function () {alert (this.name+ ' sex ' +this.sex+ ' say Hello '); }} var ming = new People (' Y ', ' nan '); Ming.show ();

  

The second Kind

var People1 = {name: ' Ming ', show:function () {alert (this.name);}} People1.show ();

  

The third type: Use prototype to add attributes or methods to an object

function Cicletwo (r) {THIS.R = R;} CicleTwo.prototype.area = function () {Console.log (' second type: ' +math.pi * THIS.R * THIS.R);}; Test var newcicletwo = new Cicletwo (3); Newcicletwo.area ();

  

Fourth type

var People2 = new Object (); People2.name = ' A '; People2.show = function () {alert ("OK");} alert (People2.name);

  

Here, use People1 to inherit people.

function people (name, sex) {this.name = Name;this.sex = Sex;this.show = function () {alert (this.name+ ' sex ' +this.sex+ ' say Hello ');}} var ming = new People (' Y ', ' nan ');//Ming.show (); var People1 = {name: ' Ming ', show:function () {alert (this.name);}} People1.show (); People1.show.apply (Ming);//eject y instead of Ming, stating that the name inherited from people y

  



It's here today, it's too late, tomorrow we'll tidy up the data type knowledge points.

Information to be consulted here:

Http://www.cnblogs.com/KeenLeung/archive/2012/11/19/2778229.html

========================================================

6-22 11 points continue to learn: JS data type, parameter transfer problem.

Raw data type (5 types)

1. Number

2, String

Is the only primitive type that does not have a fixed size.

Because we all know that the value of the original type is stored in the stack, because the size is fixed, the memory is small, the value of the reference type exists in the heap, and the referenced variable is the address of its value, the address is fixed, so there is a stack, but its value is in the heap, and the variable is its address.

3, Undefind

When a variable is declared without initialization, the variable is not defined

var Otemp;alert (typeof Otemp);  Output "undefined" alert (typeof oTemp2);  Output "undefined"

  

The preceding code outputs all two variables as "undefined", even if only the variable OTEMP2 has never been declared. If you use an operator other than typeof for OTEMP2, it will cause an error, because the other operators can only be used on declared variables.

When the function has no explicit return value, the returned value is also "undefined", as follows:

function TestFunc () {}alert (testFunc () = = undefined);  Output "true"

  

4, NULL

Represents an object that does not already exist, and is usually null when the object is not found if the function or method is to return an object.

Alert (Null = = undefined);  Output "true"

  

This is because the value undefined is actually derived from the value null, so ECMAScript defines them as equal.

5, Boolean

Reference data types, common with object,array,function,date.

All the arguments are the values that are passed.

Simple raw data-transfer parameters

function Show (num) {var a = 5; num = A; alert (num);} show (2);//Return 5

A clearer example:

var a = 1;function foo (x) {    x = 2;} Foo (a); Console.log (a); Still 1, not affected by x = 2 Assignment

  

Because values are present in the stack, passed as a value, X is just a copy of a, and does not affect the a itself.

Reference type of the argument, example one

function SetName (obj) {obj.name = ' abc ';} var person=new Object (); SetName (person); alert (person.name);

  

Initializing an object Person,person points to the address of the object itself, and then sets the person object to a Name property value of ' ABC ' through the function, and always remember that the address passed in is saved with the local variable obj declared inside the function. Equivalent to assigning the person object address value to obj. Because obj and person point to the same object, the person will show up when obj changes.

Take another look at this example

function SetName (obj) {obj.name = ' abc '; obj = new Object (); obj.name = "Def";} var person=new Object (); SetName (person); alert (person.name);//abc

  

As you can see in the function, the local variable obj inside the function is re-assigned a new object address. At this point, obj does not point to the person, which of course returns ABC instead of DEF.

Resources:

Http://www.w3school.com.cn/js/pro_js_primitivetypes.asp

http://www.zhihu.com/question/27114726

http://www.zhihu.com/question/27114726

Data types in JavaScript

Http://www.jb51.net/article/29703.htm

JS data type (original type and reference type) thought by JS Apply and call method

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.