JavaScript Object-Oriented Programming Guide reading notes-function

Source: Internet
Author: User
Tags object object

4.2.3 Function

A function is a special type of data that is actually an object.

4.2.3.3 call () and apply ()

1. In JavaScript, each function has a call () and an apply () two methods.
The two features of these two methods:

(1)用他们来触发函数,并指定相关的调用参数。(2)他可以让一个对象去“借用“另一个对象的方法,并为已所用。这也是非常简单而实用的代码重用。

2. Use an example to explain call
(1) Defines a Some_obj object that has a say () Method:

var some_obj={  name: ‘peichunyan‘,  say: function(who){     return ‘Haya‘ + who + ‘, I am ‘ + this.name;  }};

In this way, you can call the object's say () method and use THIS.name to access its Name property:
some_obj.say( ‘Kitty‘); //输出:Haya Kitty, I am peichunyan
(2) below, we create a My_obj object that has only one name property:
var my_obj={ name: ‘LiYundi‘};
(3) for My_obj, also want to have and some_obj the Say () method the same function method, therefore, we want to use this method as My_obj own method to call (no need to repeat the implementation of it again). In this case, we can try the object method in the Say () function call ():

some_obj.say.call(my_obj, ‘Yanglan‘);//输出: "Haya Yanglan, I am LiYundi "

(4) Explanation: Because we passed two arguments when invoking the object method call of the Say () Function: Object My_obj and the string "Yanglan". In this way, when say () is called, the This is automatically set as a reference to the My_obj object. So we see that the this.name return is no longer "Peichunyan", but "Liyundi".
(5) If we need to pass more arguments when invoking the call method, we can join them in turn:
some_obj.someMethod.call(my_obj, ‘a‘, ‘b‘, ‘c‘);

    1. Apply ()
      Apply () works in the same way as call (), except that the parameter is passed in the form of a parameter, where the parameters required by the target function are passed through an array. Therefore, the following two lines of code are equivalent:
some_obj.someMethod.call(my_obj, ‘a‘, ‘b‘, ‘c‘);some_obj.someMethod.apply(my_obj, [‘a‘, ‘b‘, ‘c‘]);

So, for the previous example, you can also write this:

some_obj.say.apply(my_obj, [ ‘Yanglan‘]);//输出: "Haya Yanglan, I am LiYundi "
4.2.3.4 re-recognize arguments objects

1. You can access all the parameters that are required to pass to the function in a function through arguments. Chestnuts:

function f( ){   return arguments;}f(1,2,3);   //[1,2,3]

2. Although arguments looks like an array, it is actually an array-like object. It is similar to the array because it also contains the index element and the length property. But the similarity ends here, because arguments does not provide array methods like sort (), slice ().

    1. But we can convert the arguments to an array so that it can be used in a variety of arrays.
      Example:
function f( ){   var  args = [].slice.call( arguments);  return args.reverse( );}f(1,2,3,4);       //[4,3,2,1]

This is done by creating a new empty array and then using its Slice property. You can also use Array.prototype.slice to invoke the same function.

4.2.3.5 Inferred Object Types

(1) Question: Since the TypeOf return value of an array is also "object", how do you differentiate between objects and arrays?
(2) Answer: Use the ToString () method for object objects. This method returns the internal class name of the object being created.
(3) Example:

Object.prototype.toString.call( { } );        // [object Object]Object.prototype.toString.call( [ ] );        // [object Array]

Here, the toString () method must be derived from the prototype property of the object constructor. It is not possible to call the ToString () method of an array directly, because in the array object, this method has been rewritten for other purposes.
[1, 2, 3].toString( );

JavaScript Object-Oriented Programming Guide reading notes-function

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.