Understand JavaScript's apply again

Source: Internet
Author: User

When the normal function executes, this points to the context in which the function executes

is actually the structure of a prototype chain ... I've never understood the prototype chain.
Are they linked together like chains? Ngong... Can the prototype chain be understood as inheritance? It's like, the inheritance inside Java.
Yes, yes.
Just want to say that this time to write JS is generally useless here, and apply () and call () have been confused
Too flexible, do not know what, want to do what ES6 new grammar, you may look more familiar with the

Call and apply are bindings to a function that is executed by this point

Apply () is very much related to the code context

Does this mean modifying the parent class?

No.. Then I don't understand.
Modify this point for what?
What's the plot? Ngong...
I think it's a scene, huh? I understand, is not to modify the scope of implementation?

I personally think that ... The primary function of apply and call is to implement the inheritance of the prototype chain.

Like Appply (a)
Does the object in front of apply become a property? Not a property ...
Instead, the context of the function execution of this execution becomes a

You mean just doing it in a, but it's okay with a?

Other words

Xxx.apply (a)

XXX in the execution of the time you call this is actually a just use the properties of a and other data fields?

Yes, I do.

I wipe, js good bottom ah

It's like python.

C # absolutely can't do that.

But it's too much crap, don't use this.
It's OK to use a different variable.//I have a class that generates two properties, one name and one age

function person (name, age) {
THIS.name = Name
This.age = Age
}

Person.prototype.show = function () {
Console.log (' name: ', Name, ' age ', age)
}

At this point, I need a new class, I need a sex attribute outside of name and age, but the person class is reusable and there is no need to re-write the code again, so we need to use the person to perform some property building operations

Function man (name, age) {
Person.apply (this, [name, age])
}

var people = new Man (' Niko ', 18)
But this way of inheriting. There is no way to get the prototype of the function you apply to the Rough prototype address assignment past.

This is the sentence that executes the person's code, but this is actually the context of the man instantiation.

is a form of early succession.
It's a new approach for the last two years.

I understand that this is to achieve inheritance.

is to have the person initialize some properties for man

But it can also be used in some of the more surprising places, haha

That's almost what it means.

This is an interview when apply appears to be relatively high frequency

a little trick. Does the person have a property of man?

It's hard to understand.

I don't understand that.

Ngong.. Apply the second parameter is an array, which is then plugged into the arguments of the function being used.
Because Math.max only supports one parameter pass, there is no way to pass in an array
So.  is to take advantage of this feature of apply. I don't want to say anything.
I hate this kind of usage js good is fortunately ... A variety of code that you can't imagine, which means that JavaScript is not unified.
Mix grammar and execution together Ang.
You can even generate code in code and then go to execute ... Forgive me for not having enough IQ
Foolhardy meta programming?  is to execute a string of strings as code ... The above content to organize from and a front-end Daniel's chat, thank Daniel! Almost finally understand that apply is to implement inheritance, and when executed, call the parent class to help initialize the property. Understand here first.

Understand JavaScript's apply again

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.