Explain criptcall () method in detail

Source: Internet
Author: User
The javascriptcall () method calls a function or method on the premise of using a specified this value and several specified parameter values. this article describes how to use the call () method, for more information about the coders, see. call()When using a specified thisTo call a function or method.

Call () basic syntax

fun.call(thisArg[, arg1[, arg2[, ...]]])

Call () parameters:

Parameter Name Parameter description
ThisArg InfunThethisValueNote that the specifiedthisThe value is not necessarily true when the function is executed.thisValue. If this function is in non-strict mode, it is specifiednullAndundefinedOfThis value automatically pointsGlobal Object (window object in the browser), and the value is the original value (number, String, Boolean value)thisWill point to the automatically wrapped object of the original value.
Arg1, arg2 ,... The list of specified parameters.

Call () basic functions

When calling a function, you can assign a different valuethisObject.thisReference the current object, that iscallThe first parameter of the method.

PassCall method,You can borrow methods from another object, suchObject.prototype.toString.call([]),IsArrayThe object has been borrowed.Methods on the Object.

Call () instance 1: call the parent constructor using the call Method

In a sub-constructor, you can callcall Method To implement inheritanceSimilarJava. In the following exampleFood AndToy The object instance created by the constructor will haveProduct The name and price attributes added in the constructor,category Attributes are defined in their constructors.

function Product(name, price) {  this.name = name;  this.price = price;  if (price < 0) {    throw RangeError('Cannot create product ' +                      this.name + ' with a negative price');  }  return this;}function Food(name, price) {  Product.call(this, name, price);  this.category = 'food';}Food.prototype = Object.create(Product.prototype);Food.prototype.constructor = Food; // Reset the constructor from Product to Foodfunction Toy(name, price) {  Product.call(this, name, price);  this.category = 'toy';}Toy.prototype = Object.create(Product.prototype);Toy.prototype.constructor = Toy; // Reset the constructor from Product to Toyvar cheese = new Food('feta', 5);var fun = new Toy('robot', 40);

Call () instance 2:UsecallMethod call anonymous Function

In the following exampleforIn the loop body, we create an anonymous function and then callCall method,Use each array element as the specifiedThis valueThe anonymous function is executed. The main purpose of this anonymous function is to addprintMethod, thisprintMethod to print the correct index number of each element in the array. Of course, it is not necessary to make the array elementthisThe value is passed into the anonymous function (normal parameters are acceptable) to demonstratecall.

var animals = [  {species: 'Lion', name: 'King'},  {species: 'Whale', name: 'Fail'}];for (var i = 0; i < animals.length; i++) {  (function (i) {     this.print = function () {       console.log('#' + i  + ' ' + this.species + ': ' + this.name);     }     this.print();  }).call(animals[i], i);}

Call () instance 3:UsecallMethod to call an anonymous function and specify the 'eas' of the context'

In the following examplegreet Methodthis The value is boundi Object.

function greet() {  var reply = [this.person, 'Is An Awesome', this.role].join(' ');  console.log(reply);}var i = {  person: 'Douglas Crockford', role: 'Javascript Developer'};greet.call(i); // Douglas Crockford Is An Awesome Javascript Developer

Note:Functions andapply()The method is similar. There is only one difference:call()The method acceptsList of several parameters, Andapply()The method acceptsAn array containing multiple parameters.

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.