In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about the basic functions of JavaScript, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
First, the introduction of the function 1.1. The creation of function
1. Three ways to create a function
/ / method 1: string encapsulation code creation function var fun = new Function ("console.log ('hello I am the first function'); console.log (fun)") / / function executes fun () / / method 2: function declaration function fun2 () {console.log (fun2)} fun2 () / / method 3: function expression var fun3 = function () {console.log ("I am the encapsulated code of an anonymous function")}; console.log (fun3) fun3 (); 1.2. Parameters and return values of the function
1. The argument to a function can be an object or a function
Function mianji (r) {return 3.14 * r * r;} function fun (a) {console.log ("a =" + a)} fun (mianji (10)) fun (mianji)
two。 The return value of a function can be an object or a function:
Function fun4 () {function fun5 () {alert ("I am fun5")} return fun5;} a = fun4 (); console.log (a) / output fun5 function body a () / / output "I am fun5" fun4 () () / / output "I am fun5" II. Constructor function Person (name, age, gender) {this.name = name; this.age = age; this.gender = gender; this.sayName = function () {alert (this.name)} } var per = new Person ("Zhang San", 15, "male") var per2 = new Person ("Li Si", 16, "female") var per3 = new Person ("Wang Wu", 17, "male") console.log (per)
Constructor execution flow:
1. Create an object immediately
two。 Set the newly created object to this in the function
3. Execute the code in the function line by line
4. The maximum return value of the newly created object is returned.
Summary: as for the constructor above, it must be called with the new keyword, and the ordinary function must be called directly, using this instead of obj.
2.2. Immediately execute the function (function () {alert ("I am an anonymous function")}) / / the function is called immediately after it is defined, which is called immediate execution function 2.3. Prototype prototype of the constructor
1.JavaScript states that every constructor has a prototype property.
two。 The function assigned by the constructor through the prototype is shared by all objects.
3. We can define those immutable methods directly on the prototype object so that all instances of the object can share these methods.
Summary: each object has a property proto that points to the prototype prototype object of the constructor, and the instance object uses the properties and methods of the constructor prototype prototype object, because the object has a proto prototype.
2.4. The this in the function points to
1. When called as a function, this is window
two。 When called in the form of a method, whoever calls the method this is the one.
3. When called in the form of a constructor, this is the newly created object.
2.5. The method of function object prototype
1.Function.prototype.call (): calls a function with a specified this value (simply understood as the way the function is called, but it can change the this direction of the function)
The 2.Function.prototype.apply () method calls a function. It is simply understood as the way the function is called, but it can change the this direction of the function.
3. The Function.prototype.bind () method does not call the function, but it can change the internal this direction of the function and return the new function generated after the original function changes this.
/ / 1.call method var o = {name: "Zhang Mei"} function fn (a this b) {console.log (this); console.log (a + b) } fn (1 2.apply 3) / / the this at this time points to the result of the window operation: 3 fn.call (the this at this time points to the object o running result: the output object o and the 3 / / 2.apply method var o = {name:'andy'} function fn1 (a) B) {console.log (this) Console.log (a + b)} fn () / / the this at this time points to the object o pointed to by the window running result: 3 fn.apply (o, [1J 2]) / / the this at this time Run result: output object o and 3 / / 3.bind method var o = {name:'andy'} function fn1 (a console.log b) {console.log (this) Console.log (a + b)} var f = fn.bind / / where f is the new function returned by bind f () / / calling the new function this points to object o2.6. Inheritance of constructor
1. Define a parent constructor first
two。 Define another subconstructor
3. The child constructor inherits properties from the parent constructor (using the call method)
/ / 1. The parent constructor function Father (uname) {/ / this points to the object instance of the parent constructor this.uname = uname;} / / 2. The subconstructor function Son (uname, age) {/ / this points to the object instance / / 3 of the subconstructor. Use call to implement the attribute Father.call (this.uname) of the child inheriting the parent; this.age = age;} var son = new Son ('Wang Shuai', 18); console.log (son); / / Son {age:18} 2.7. The prototype object prototype method inherits / / 1. The parent constructor function Father (uname, age) {/ / this points to the object instance of the parent constructor this.uname = uname; this.age = age;} Father.prototype.money = function () {console.log (100000);}; / / 2. The child constructor function Son (uname, age, score) {/ / this points to the object instance of the child constructor Father.call (this, uname, age); this.score = score;} / / Son.prototype = Father.prototype; will have a problem with direct assignment. If the child prototype object is modified, the parent prototype object will also change Son.prototype = new Father () / / if you modify the prototype object in the form of an object, don't forget to use constructor to refer back to the original constructor Son.prototype.constructor = Son; / / this is the subconstructor-specific method Son.prototype.exam = function () {console.log ('children want to take an exam');} var son = new Son ('handsome Wang', 18100); console.log (son) / / there is a Father prototype object, and there is an age,uname,exam;money in the Father in the fahter outer prototype object. 2.8. Object.create implements class inheritance / / parent class function Shape () {this.x = 0; this.y = 0;} / subclass function Rectangle () {Shape.call (this); / / calls the constructor of the parent class} / / takes the parent class prototype object as the child class prototype object. The subclass inherits the parent class Rectangle.prototype = Object.create (Shape.prototype); / / sets the subclass prototype constructor Rectangle.prototype.constructor = Rectangle;var r = new Rectangle (); r instanceof Rectangle; / / truer instanceof Shape; / / true after reading the above, do you have any further understanding of the basic functions of JavaScript? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.