Wonderful js_javascript skills

Source: Internet
Author: User
Tags define function hasownproperty
Fantastic js The Code is as follows:


>>> Function a () {function B () {return "aaa"} Function. prototype. c = function () {return B ();}}
>>> ()
>>> A. c
Function ()
>>> A. c ()
"Aaa"
>>> A. hasOwnProperty ("c ")
False


Look at this code. First, declare a function a and define function B internally. However, function B is not the method of function object, it's just a temporary variable function (or a private function, but I don't know how to describe it) in function a. a function c uses function () is defined later () {} is defined internally in a, so closure is generated. Therefore, c can traverse all the internal block variables under a, including B. I mounted c to Function again. under prototype, a is not directly attached to a, but to the prototype chain of a. It is finally executed, and hasOwnProperty is also false.

Code

The Code is as follows:


>>> D = {};
Object
>>> Function a () {function B () {return "aaa"} d. c = function () {return B ();}}
>>> ()
>>> D. c ()
"Aaa"



Closure has nothing to do with the context of function execution. context can change this using the call apply method. However, closure does not seem to be able to modify the function after the function is defined.
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.