Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the little knowledge points of javascript?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the little knowledge points of javascript". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the little knowledge points of javascript"?

When setting a picture, only one length or width is set, and the rest of the side length is automatically set proportionally.

When the mouse moves over the picture, it becomes a small hand to add style cursor:pointer to the picture.

Prevent form submission

When setting the weight of the box, write z-index in the style: the higher the number, the higher the priority.

The difference between an id selector and a class selector: a tag on the same page can only correspond to one id, but you can select multiple classes, and classes can also be applied to multiple tags.

Label a href= "_ javascript:;" where _ javascript: is a pseudo protocol, which allows us to call the javascript function through a link. In this way, _ javascript:; can achieve the click event of the A tag when running, if the page has a lot of content, when there is a scroll bar, the page will not jump, and the user experience will be better. It is equivalent to executing an empty javascript code, and the address does not jump. Same as this one.

Remove the dot in front of li and use list-style-type: none

Make the shape of li or span round with border-radius: 50%; add a fillet border.

Textarea setting resize:none; prevents users from changing the size of the text box.

I want the box to change the size of the box from height to auto according to the size of the contents.

Make buttons or forms unavailable this.disable=true

Return an if an is false, or b if an is true.

I. a | | b if an is false, b if an is true.

Img {vertical-align: top;} / cancel the bottom 3 pixel distance of the picture /

It is important to note that when you modify the style style in javascript, the assignment should be in the form of a string.

After javascript modifies the values in style, you can see them in the inline numeric style.

When using webstorm to create a new javascript file, first change the encoding format in the lower right corner to utf-8, otherwise later running will make an error. The same goes for new css files.

If you reference a javascript file in head, you need to consider whether the functions in the file are in _ window.onload. Otherwise, an javascript error will occur because the web page has not finished loading.

The input in the form can be obtained directly from the DOM object, form. Form, through the name property. Control name

When creating a string, it is best to enclose the tagged string in single quotation marks to avoid conflicts with the double quotation marks inside.

JS page jump: _ window.location.href = "http://www.itcast.cn".

To get the current page width, use document.documentElement.clientWidth.

Get the relative scrolling document.documentElement.scrollTop of the current page | | document.body.scrollTop

When the type of input is changed to search, enter can be used as the confirmation key.

Get the screen touch with addEventListener ("touchstart", function (event) {}) use event.touches [0] .clientY; in the function to obtain the Y coordinates of the mouse click and touchmove to obtain the last coordinates of the mouse when the touch leaves.

Use var img=new Image (width,height); the effect is the same as that of cratelement. Use img.src to add pictures.

Converting strings to JSON objects uses the global JSON function JSON.parse (string)

You can use instanceof to determine the constructor of an object.

If return is added to the function, it cannot be treated as a constructor. The constructor return is an object.

Typeof and call are often used to determine data types, and when using the typeof operator to store values (NULL, array, object) with reference types, there is a problem that it returns "object" no matter what type of object is referenced.

Console.log (toString.call ({})) / / [object Object]

Console.log (toString.call ([])) / / [object Array]

Use window.addListener ("hashchange", function (e) {}) to listen for hash changes after # in the address bar, and use _ window.location to get the address object.

Parsing an address first creates an an object var aLink=document.creatElement ("a")

Then the a label object href is assigned an address string, and then the address hash value can be obtained through aLink.hash

Tips for constantly adding content to an array can arr [arr.length] =... To constantly assign values to the array

Delete elements from the array can also be splice (element index, delete the number of elements).

For loops arrays or objects using (index in arry) as a conditional index to represent the current element index.

You can use externally linked tags such as img (supports cross-domain but cannot obtain server data), iframe (can accept complex single operation of data), link (errors will be reported in the CSS processing phase), and script (can obtain server data and is chosen as the jsonp transmission method).

In order to make the code easy to maintain and adopt the open-close principle, the amount that will change later must not be written down, and it needs to be iterated through modification later.

Change point encapsulation, generally the address class that will change in the future is written in the config function, followed by aliases to use. Be sure not to expose it to the code.

When an object is assigned to another object using "=", a memory object is passed. If you modify the properties of one of them, it will change. To avoid tampering with the source object, you can use the object.assign () method or the object.creat () method.

A function object in js has a length, and its length is determined by the number of parameters. (function (a, b, c, d) {}) .length = = 4.

To properly handle the uploaded file and accept the contents of the file, you need to set the enctype property of the form to multipart/form-data, a MIME type for BLOB (large binary files).

To save an array of objects to a file as a string, you need to loop through the array and convert the object to a string using the .stringify method of the JSON object, and then save it.

You can use gulp+browersync (yo webapp generation template) to write static web pages and express+ejs+sass.

Sass listening folder: sass-- watch sassFileDirectory:cssFileDirectory

Sass listens for a single file: sass-- watch style.scss:style.css

Currying of a function is a technique that transforms a function that accepts multiple parameters into a function that accepts a single parameter (the first parameter of the original function), and returns a new function that accepts the remaining parameters and returns the result. It not only increases the applicability of the function, but also reduces the scope of application of the function.

Lazy loading indicates that the branch of the function execution will only be executed when the function is deactivated for the first time, and during the first call, the function will be overwritten as another function executed in an appropriate manner, so that any calls to the original function no longer have to go through the executing branch. If the function function corresponds to the XMLHttpRquest created by different browsers, after judging the browser type and creating it for the first time, you can return a modified function to overwrite the original function, so that when you call this function later, you no longer need to determine the browser type, and you can directly create the corresponding XMLHttpRquest.

The main purpose of aspect-oriented programming AOP is to extract the aspects in the business processing process, which is faced with a certain step or stage in the processing process, in order to obtain the isolation effect of low coupling between the parts of the logic process. For example, the most common one is logging.

The search, match, and replace parameters of String can all be regular expressions.

Any language will have errors in floating-point operation, which is converted to binary, calculated with binary, and then converted back to decimal, which will be converted to infinite cyclic decimal in the conversion process, so there will be errors in the final result. Number.prototype.toFixed () is used in js to set the number of decimal places, which returns a string. You can use valueOf () to convert to the number type again.

The reduction function of the array reduce (function (prev,cur,i) {}) iterates from left to right, with the value returned each time as the next prev parameter.

When iterating through the array, if you want to remove the result from the array to prevent repetition after finding the result, because if the deletion directly affects the loop of the array, the solution is to assign this value to NaN, so that it is null when displayed in the array. Use delete to delete a numeric element, delete the element will create a vacancy, length will not be updated, the vacancy section shows undefined. If you do not assign a value to an index of the array in array order, the array will be discontiguous and the middle vacancy will show undefined. ForEach () ignores undefined. Use the splice () function to remove elements without creating gaps. If you change the length of the array to a certain value, all parts of the array outside this index will be deleted. Use array.filter (function (v) {return true;}) to filter out vacant functions.

When using closures, it is important to note that because variables are kept in memory, it consumes memory, so closures cannot be abused. The solution is to delete all unused local variables before exiting the function.

Use array.filter (function (val) {}) to traverse all the contents in the array, return true's array retains the current content, and false removes the current content.

Get all the parameters passed into an array args = Array.prototype.slice.call (arguments)

Note when writing recursive functions: no matter whether the conditions for jumping out of recursion are met or not, the next step must be executed with return, that is, you must bring return before calling yourself, and you can only jump out of the loop step by step when you meet the conditions for jumping out of the loop.

Var lefted,sliced = []; function chunkArrayInGroups (arr, size) {

object-oriented

Create a common property in the constructor through this. Create, and private attributes are created through var.

Using delete collection.tracks, you can remove the tracks attribute of collection.

Data properties have four described behavior properties, which are set by Object.defineProperty (obj,propKey,propDesc).

Writable indicates whether the value of the property can be modified. Default is false

Enumerable indicates whether the return property can be enumerated through the for in loop. The default is that false can enumerate

Configuralbe indicates whether an attribute can be deleted by delete and can never be redefined and its configuration can be modified. The default is false.

Value contains the data value of this property. When reading property values, read from this location.

Using Object.keys (obj), you can get all the own (non-inherited) enumerable property keys of the obj object, which are usually enumerable, and Object.getOwnPropertyNames (obj) returns all the property keys of the obj object.

The general rule of enumeration is that properties created by the system cannot be enumerated and attributes created by users can be enumerated. The main purpose of enumerations is to determine which attributes in the for-in loop should be ignored. For your own code, you can usually ignore enumerated properties, and you should avoid using for-in loops, whose enumerated properties are enumerable by default.

There are three ways to protect objects:

Prevent extension: Object.preventExtensions (obj); setting objects cannot add properties.

Closure: Object.seal (obj); the value of an attribute can be changed, but the property of an attribute cannot be changed.

Freeze: Object.freeze (obj) makes all properties unwritable and the closed obj cannot be extended.

The An instanceof B operator can be used to determine whether the prototype property of the B constructor exists on the prototype chain of the An object. Equivalent to B.prototype.isPrototypeOf (A)

Another way to set up a prototype is Object.creat (proto,propDescObj?).

Another way to get prototypes: Object.getPropertypeOf (obj)

Realize modular programming

/ / the computing module calc_v2015.js has come to this point. I believe you have a deeper understanding of "what are the little knowledge points of javascript?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report