How to avoid the top ten errors in JS
This error is the same as chrome’s “TypeError: ‘undefined’ is not a function”. It’s just that different browsers will report different error words. This kind of error is generally high on IE that uses namespaces. 99.9% it’s because IE can’t resolve the correct namespace that this points to. For example: var Person = { name : “daisy”, getName : function() { console.log(this.name) }, print: function() { this. getName() } }; For example, in the namespace of Person, the method this.getName() can be called in print. But it doesn’t work in IE, so the namespace must be clearly stated. var Person = { name : “daisy”, getName : function() { console.log(Person.name) }, print: function() { Person. getName() } }; (Note: Since I don’t have a Windows computer at hand, and I’m too lazy to find ==, so I haven’t verified it. According to the translation of the original text, I understand this meaning. If you are interested, you can verify it. Comments Tell me the conclusion~) 6. TypeError: ‘undefined’ is not a function This is the reason mentioned above, did Chrome/Firefox call it? The defined method leads to. I won’t go into details. Of course, except for negligence, no one will…
Do I need to add a semicolon to the JS code?
Semicolons in Javascript separate communities. Some people like to use semicolons anyway. Others prefer not to include semicolons. After years of using semicolons, in the fall of 2017, I decided to try going without semicolons as needed, and set Prettier to automatically remove semicolons from my code unless necessary code structures required them. Now I find it very natural not to use semicolons, I think such codes look better, they are more concise and readable. This is entirely possible because Javascript does not strictly require semicolons. When a semicolon is required somewhere, it adds it behind the scenes. This process is called automatic semicolon insertion. It is important to understand the rules for using semicolons so that you can avoid writing buggy code because they behave as you expect s difference. Javascript automatically adds semicolon rules Javascript interpreter finds the following special circumstances when interpreting the source code, it will automatically add semicolons: When the beginning of the next line of code interrupts the current line of code (the code can be written in multiple lines) The next line starts with }, closing the current block When the end of the source code is reached When the return statement is…
How does JS get the value of input?
var inputEl = document.querySelector('[name=”username”]'); var val = inputEl. value; console.log(val); input attribute HTML5 property value description accept mime_type Specifies the type of file to submit via file upload. align left right top middle bottom Deprecated. Specifies the alignment of the image input. alt text Define the image input Alt text. autocomplete on off Specifies whether to use the autocomplete function of the input field. autofocus autofocus Determines whether the input field gets focus when the page loads. (not applicable to type=”hidden”) checked checked Specifies that this input element should be selected when it is first loaded. disabled disabled When the input element is loaded disable this element. form formname Specifies one or more forms to which the input field belongs. formaction URL Overrides the form’s action attribute. (for type=”submit” and type=”image”) formenctype see note Overrides the form’s enctype attribute. (for type=”submit” and type=”image”) formmethod get post overrides the method attribute of the form. (for type=”submit” and type=”image”) formnovalidate formnovalidate Overrides the form’s novalidate attribute. If this attribute is used, no validation will be performed when the form is submitted. formtarget _blank _self _parent _top framename Overrides the form’s target attribute. (for type=”submit” and type=”image”) height pixels % Defines the height…
js object-oriented programming
1. Object-oriented programming 1. Process-oriented and object-oriented 1) Process-oriented: Focus on the process steps of how to solve a problem. The programming feature is to realize each step of the process step by function. There is no class and Object concept. 2) Object-oriented: Focus on which object solves the problem. The programming feature is that a class appears, the object is obtained from the class, and the object solves the specific problem. For the caller, process-oriented requires the caller to implement various functions by himself. Object-oriented, only requires the caller to understand the function of the specific method in the object, and does not need to understand the implementation details in the method. 2. Three Object-Oriented Features Three Object-Oriented Features Large feature inheritance, encapsulation, polymorphism. JS can simulate inheritance and encapsulation, but it cannot simulate polymorphism, so JS is an object-based language, not an object-oriented language in the traditional sense. 3. The relationship between classes and objects 1) Classes are abstract, objects are concrete (classes are the abstraction of objects, objects are the concretization of classes) 2) Classes are an abstract concept, only It can be said that classes have attributes and methods, but they cannot assign specific properties to…
5 bad habits of JS coding, how to avoid them?
Have you ever had this feeling when reading Javascript code You hardly understand the code role? The code uses a lot of Javascript tricks? Naming and coding style too arbitrary? These are all symptoms of bad coding habits. In this article, I describe 5 common bad coding habits in Javascript. Importantly, this article will give some actionable suggestions on how to get rid of these habits. 1. Do not use implicit type conversion Javascript is a A loosely typed language. When used correctly, this is a benefit because it gives you flexibility. Most operators + – * / == (excluding ===) perform implicit conversions when dealing with operands of different types. The statements if(condition) {…}, while(condition){…} implicitly convert the condition to a Boolean value. The following example relies on implicit conversion of types, which can sometimes be confusing: console.log (“2” + “1”); // => “21” console.log(“2” – “1”); // => 1 console.log('' == 0); // => true console.log(true == []); // -> false console.log(true == ![]); // -> false Relying too much on implicit type conversions is a bad habit. First, it makes your code less stable in edge cases. Second, there is an increased chance of introducing bugs that…
Array methods commonly used in JavaScript
some() method This method checks whether at least one element of the array satisfies the conditions checked by the parameter function. Output: true reduce () method The array reduce () method in Javascript is used to reduce the array to a single value, and for the array Each value of (from left to right) and return value executes a provided function. Functions are stored in the accumulator. Output: 3 map() method The map() method in Javascript works by calling a specific function to create an array. This is a non-mutating method. Typically, the map() method is used to iterate over the array and call a function on each element of the array. Output: true flat() method This method creates a new array that contains multiple arrays. Basically create a simple array from an array containing multiple arrays. Output: 11, 89, 23 ,7,98 flatMap() method This method is used to flatten the input array elements into a new array. This method first maps each element with the help of the map function, then flattens the input array elements into a new array. Output: 112, 52, 944 findindex() method This method returns the first in the given array that satisfies the provided…
Singleton mode of JavaScript design pattern
Preface In the course of Javascript development, the pioneers have summed up many solutions to specific problems from practice. Simply put, a design pattern is a concise and elegant solution to a specific problem on a certain occasion In the future, I will record various common design patterns in Javascript. Maybe you are already familiar with it, maybe you are already using it, but you are not particularly familiar with its concept, or maybe you just have some vague concepts about it. Well, I believe this series will definitely bring you some gains Before understanding these common modes, by default you have at least mastered this closure higher order function Prototype and prototype chain Understanding them will give you a clearer understanding of a pattern. Of course, maybe the things I recorded about this aspect before can give you some help. Portal If there are any mistakes or mistakes in the article, please give advice to the friends who have seen it, thank you in advance Below, let’s start with it — the singleton pattern Concept As the name suggests, there is only one instance Definition: Ensure that a class has only one instance, and provide a global access point…
Can PHP methods be written in jQuery?
jquery php Write your review! Spit it out, see everything Member Login | User Registration recommended reading js The event is triggered when php first loads the page, and there is no other time after that My idea is that I want to prompt the user to change the password when the page loads using jQuery’s: $(‘a’).trigger(‘click’); The problem I am facing is that this is triggered every time the page loads Event. Ideal Situation… [detailed] Crayon Shinchan 2023-08-27 12:20:18 js In the novel “Lin Haixueyuan”, the most powerful bandit, Zheng Sanpao The “Linhaixueyuan” that we all know is the most powerful antivirus software in the movie “Linhaixueyuan” starring Zhang Yongshou, Wang Runshen, Zhang Liang, Liang Zhipeng, Liu Jiyun, Yang Chengxuan, Zhang Chi, Shi Wei, etc. … [detailed] Crayon Shinchan 2023-08-27 12:23:23
Three ways to implement Fibonacci columns in JS
The following is the Javascript basics tutorial column to introduce the three methods of JS to realize the Fibonacci sequence number, I hope it will be helpful to friends in need! Three methods of implementing Fibonacci columns in JS How do you realize the Fibonacci series 1,1,2,3,5,8… f(n)=f(n-1) + f(n-2) Method 1: function f(n){ if(n == 1 || n == 0){ return 1; } return f(n-1) + f(n-2); } index.html Give two more solutions for comparison Method 2: function f(n ) { var arr = []; var value = null; function _f(n) { if (n == 1 || n == 0) { return 1; } if (arr[n]) return arr[n]; value = _f(n – 1) + _f(n – 2); arr[n] = value; return value; } return _f(n); } Method 2 Another simpler way is to use array storage Method 3: function fn(n) { var dp = new Array(n + 1); dp[0] = dp[1] = 1; for (let i = 2, length = dp.length; i <length; i++) { dp[i] = dp[i – 1] + dp[i – 2]; } return dp[n]; }
Several things JavaScript objects can do
Besides normal object property assignment and traversal, we can perform many other operations with Javascript objects. In this article, we’ll see how to use them, including accessing internal properties, manipulating property descriptors, and inheriting read-only properties. 1. Accessing internal properties Internal properties of Javascript objects that cannot be accessed in the normal way. Internal property names are surrounded by square brackets [[ ]] and are available at object creation time. Internal properties cannot be dynamically added to existing objects. Internal properties are available on certain built-in Javascript objects that store internal state as specified by the ECMAScript specification. There are two internal properties, a method for manipulating objects, and a method for storing data. For example: [[Prototype]] — prototype of the object, can be null or object [[Extensible]] — Indicates whether new properties are allowed to be dynamically added to the object [[PrivateFieldValues]] — Used to manage private class fields 2. Attribute Descriptor Object The data attribute contains the location of a data value, which can be read and written value. That is to say, data attributes can be accessed through object.attribute, that is, what value is assigned by the user we usually contact, they will return what is, and…