Home > Web Front-end > JS Tutorial > body text

Share Javascript practical method 2_javascript skills

WBOY
Release: 2016-05-16 15:25:53
Original
1072 people have browsed it

JavaScript is a literal scripting language. It is a dynamically typed, weakly typed, prototype-based language with built-in support for types. Its interpreter is called the JavaScript engine, which is part of the browser and is widely used in client-side scripting languages. It was first used on HTML (an application under Standard Universal Markup Language) web pages to add dynamic functions to HTML web pages. .

Continuing from the previous article,

Object

keys

The keys method of object can obtain all the keys (key/property names) of a given object and return them in the form of an array. This method can be used for key filtering, matching, etc.

var basket = {
strawberry: 12,
banana: 20,
apple: 30,
juice: 20
};
console.log(Object.keys(basket)); 
//[ 'strawberry', 'banana', 'apple', 'juice' ]
Copy after login

create

The create method is used to create a new object, with optional parameters (proto, [ propertiesObject ]). The first one is the prototype, such as Array.prototype, and the second one is some new properties that need to be given to the new object. Like this, the attribute name of this parameter object will be the attribute of the new object, and the value will be the attribute descriptor (value, writable, configurable, etc.).

var o = Object.create({}, {p: {value: 42}});
var O = Object.create({}, {p: {value: 66, writable: true, enumerable: true}});
console.log(o.p); //42
console.log(O.p); //66
o.p = 20;
O.p = 80;
console.log(o.p); //42
console.log(O.p); //80
Copy after login

Writable in the attribute descriptor defaults to false, so o.p cannot change its value even if it is reassigned later, while O.p can change its value later. In addition, the create method proto must pass in the corresponding parameters, otherwise a TypeError will be reported , of course the above code will also report an error in strict mode, because o.p is rewritten - -

assign

The assign method, a new feature of es6, supports parameter passing (target, ...sources), which is used to add the key-value pairs of any number of source objects to the target object, similar to the extendOwn method of lodash's assign and underscore.

var boy = {handsome: true, rich: true}, girl = {cute: true, hair: 'long'};
var couples = Object.assign({}, boy, girl);
console.log(couples); //{ handsome: true, rich: true, cute: true, hair: 'long' }
Copy after login

The assign method is often used for data processing at the framework level. For example, if you define a client to send HTTP requests, you may have to add some default attributes in addition to the received parameters when using it.

Number

isNaN

Number’s isNaN method is used to determine whether the incoming value is a NaN value. Unlike the global isNaN method, it does not force the incoming parameter to be converted into a numeric type. Only when the parameter is a real numeric type, and True will only be returned when the value is NaN. However, as far as I am concerned, the global isNaN is used more often to determine whether a string only contains numbers,

console.log(isNaN('123f')); //true
console.log(isNaN('123')); //true
Copy after login

In addition, the isFinite(value) method is used to determine whether the incoming parameter is a finite number, and the isInteger(value) method is used to determine whether the incoming parameter is an integer.

toFixed

The toFixed method is used to convert numbers into specific strings. It supports passing in parameters (digits), 0 < digits <= 20, and will be automatically rounded and supplemented with 0 during conversion.

var cool = 666.666;
console.log(cool.toFixed(1)); //666.7
console.log(cool.toFixed(6)); //666.666000
Copy after login

A lot of things happened during this period. From Hangzhou, where I stayed for 116 days, I came to Beijing and started a new work and life. Reluctance, melancholy, excitement, excitement and other emotions are intertwined... The seven wolves got to know the other six wolves and cherished the days when everyone worked hard and had fun together. I especially remember climbing Baoshi Mountain at night, overlooking the West Lake, English poor, hahaha...

ps: javascript split() definition and usage

The split() method is used to split a string into an array of strings.

Grammar

stringObject.split(separator,howmany)
Copy after login

参数 描述
separator 必需。字符串或正则表达式,从该参数指定的地方分割 stringObject。
howmany 可选。该参数可指定返回的数组的最大长度。如果设置了该参数,返回的子串不会多于这个参数指定的数组。如果没有设置该参数,整个字符串都会被分割,不考虑它的长度。
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!