Share the Javascript practical method 2_javascript tips-js tutorial

Source: Internet
Author: User
Javascript is a literal-literal scripting language. It is a dynamic, weak, prototype-based language with built-in support types. This article will share with you the practical javascript method 2, if you are interested, let's take a look at JavaScript, a literal translation scripting language. It is a dynamic, weak, prototype-based language with built-in support types. Its Interpreter, called the JavaScript engine, is a part of the browser and is widely used in the script language of the client. It was first used on HTML (an application under the standard General Markup Language) web pages, it is used to add dynamic functions to HTML webpages.

Undertake the previous article,

Object

Keys

The object keys method can obtain all keys (key/attribute names) of a given object and return them as arrays. This method can be used to filter and match keys.

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

Create

The create method is used to create a new object. An optional parameter (proto, [propertiesObject]) is used. The first parameter is a prototype, such as Array. prototype and the like. The second is some new attributes that need to be added to the new object. The attribute name of this parameter object is the attribute of the new object, the value is the attribute Descriptor (value, writable, retriable, etc ).

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

The default value of writable In the attribute descriptor is false, so o. p cannot change its value even if it is re-assigned. p can change the value later. In addition, the create method proto must input the corresponding parameter; otherwise, a TypeError is reported. Of course, the above Code also reports an error in strict mode, because o. p is overwritten --

Assign

Assign Method, new features of es6, support for parameter passing (target ,... Sources) is used to add the target object to any source object's key-value pairs, similar to the lodash assign and underscore extendOwn methods.

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' }

The assign method is often used for data processing at the framework level. For example, if you define a client to send an HTTP request, you may have to add any default attribute other than the accepted parameters when using it.

Number

IsNaN

The isNaN method of Number is used to determine whether the input value is a NaN value. Unlike the global isNaN method, it does not forcibly convert the input parameter to a numeric type, true is returned only when the parameter is of the true numeric type and its value is NaN. However, for itself, the global isNaN is used to determine whether a string contains only numbers,

console.log(isNaN('123f')); //trueconsole.log(isNaN('123')); //true

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

ToFixed

The toFixed method is used to convert a number into a specific string. The input parameter (digits) and 0 <digits <= 20 are supported. during conversion, the value is automatically rounded to and supplemented with 0.

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

During this time, many things have happened. After 116 days in Hangzhou, I came to Beijing to start my new work and life. Perseverance, melancholy, excitement, excitement, and other emotions are intertwined... Seven Wolves, I have known the other six wolves, and I cherish these days when everyone worked together. I especially remember crawling the gem mountains 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 a string array.

Syntax

stringObject.split(separator,howmany)

Parameters Description
Separator Required. String or regular expression, which separates stringObject from the specified place.
Howmany Optional. This parameter specifies the maximum length of the returned array. If this parameter is set, no more substrings are returned than the array specified by this parameter. If this parameter is not set, the entire string is split, regardless of its length.
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.