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

Single built-in object

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

Definition: Objects provided by the ECMAScript implementation that do not depend on the host environment and exist before the ECMAScript program executes.

This means that developers do not have to instantiate built-in objects explicitly because they are already instantiated. Examples are Object, Array, and String. ECMA-262 also defines two monolithic built-in objects: Global and Math.

Global object

In a sense, it is a bottom-up object, and all properties and methods defined in the global scope are properties of Global. Such as isNaN(),isFinite(),parseInt(),parseFloat().

1. URI encoding method

encodeURI() is used primarily for entire URIs

encodeURIComponent() is primarily a segment of a URI

The main difference: encodeURI() does not encode special characters that belong to URIs, such as colons and forward slashes. encodeURIComponent() encodes all non-standard characters

1 var uri = "http://www.wrox.com/illegal value.html#start";2 console.log(encodeURI(uri)); //http://www.wrox.com/illegal%20value.html#start3 console.log(encodeURIComponent(uri)); 4 // http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.html%23start

The two methods corresponding to encodeURI() and encodeURIComponent() are decodeURI() and decodeURIComponent()

1 var uri = "http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.html%23start";2 console.log(decodeURI(uri)); //http%3A%2F%2Fwww.wrox.com%2Fillegal value.html%23start3 console.log(decodeURIComponent(uri)); //http://www.wrox.com/illegal value.html#start

2. eval() method

The eval() method acts like a complete ECMAScript parser, accepting only one parameter, the ECMAScript string to execute.

1 eval("alert('hi')");

This is equivalent to executing the following code

1 alert("hi");

It parses the arguments passed in as if they were actual ECMAScript statements, and inserts the execution results back into place. Code executed via eval() is considered part of the execution environment containing the call, so code executed via eval()

Executed code can reference variables defined in the containing environment.

1 var msg = "hello word";2 eval(alert(msg));3 4 eval("function sayHi(){alert('hi')}");5 sayHi();

Any variables and functions created in eval() are not promoted because they are contained in a string when parsing code, and they are created only when eval() executes. And in strict mode, any variables and functions created in eval() are not accessible externally

1 console.log(sayHi()); //sayHi is not defined2 eval("function sayHi(){alert('hi')}");

3. Properties of the Global object

attribute description attribute description undefined special value undefined Date constructor DateNaN special value NaNRegExp constructor RegExpInfinity special value InfinityError constructor ObjectEvalError constructor EvalErrorArray constructor ArrayRangeError constructor RangeErrorFunction constructor FunctionReferenceError constructor ReferenceErrorBoolean constructor BooleanSyntaxError constructor SyntaxErrorString constructor StringTypeError constructor ErrorNumber constructor NumberURIError constructor URIError

4. Window object

This global object is implemented as part of the window object in Web browsers, so all variables and functions defined in the global scope become properties of the window object.

1 var color = "red";2 function sayColor(){3 console.log(window.color);4 }5 window.sayColor(); //"red"

Another way to get the Global object is to use the following code:

1 var global = function(){2 return this;3 }();

Math object

ECMAScript also provides a common place to store mathematical formulas and information, namely Math objects, which provide much faster computational functions than we can write in JavaScript.

1. Attributes of Math objects

The natural logarithm of Math.LN1010 Math.LN22 Math.LOG2E Base 2 Logarithm of e Math.LOG10E Base 10 Logarithm of e Math.PIπ Square root of Math.SQRT1_21/2 Square root of Math.SQRT22

min() and max() methods

1 var max = Math.max(3,56,98,32);2 console.log(max); //983 4 var min = Math.min(3,56,98,32);5 console.log(min); //3

To find the maximum or minimum value in an array, use the apply() method as follows

1 var newArray = [1,2,3,6,5,4,8,9];2 var max = Math.max.apply(Math,newArray);3 console.log(max);

3. Rounding method

Math. ceiling () performs rounding up

Math.floor() performs rounding down

Math.round() performs standard rounding

1 alert(Math.ceil(25.9));//26 2 alert(Math.ceil(25.5));//26 3 alert(Math.ceil(25.1));//26 4 5 alert(Math.floor(25.9));//25 6 alert(Math.floor(25.5));//25 7 alert(Math.floor(25.1));//25 8 9 alert(Math.round(25.9));//2610 alert(Math.round(25.5));//2611 alert(Math.round(25.1));//25

Random() method

Returns a random number greater than or equal to 0 and less than 1

value = Math.floor(Math.random() * total number of possible values + first possible value)

Select a number between 2 and 10

1 var num = Math.floor(Math.random() * 9 + 2);2 console.log(num);

In most cases, it is possible to calculate the total number of possible values and the first possible value by a function, such as

1 function selectForm(lowerValue,upperValue){ 2 var choices = upperValue - lowerValue + 1; 3 return Math.floor(Math.random() * choices + lowerValue); 4 } 5 var num = selectForm(2,10); 6 console.log(num); 7 8 //Use this function to easily extract a random item from the array 9 10 var colors = ["red","green","blue","yellow","black","purple","brown"];11 var color = colors[selectForm(0,colors.length-1)];12 console.log(color);

5. Other methods

Math objects also contain other methods related to performing various simple or complex calculations. See table below:

Math.abs(num) returns the absolute value of num Math.asin(x) returns the arcsine of x Math.exp(num) returns the num power of Math.atan(x) returns the arctangent of x Math.log (num) returns the natural number of num Math.atan2(y,x) returns the arctangent of y/x Math.pow (num,power) Returns num to the power Math.cos(x) Returns the cosine of x Math.sqrt(num) Returns the square root of num Math.sin(x) Returns the sine of x Math.acos(x) Returns the arccosine of x Math.tan(x) Returns the tangent of x

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

Network Security

Wechat

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

12
Report