This time, we'll talk about functions and part of objects.
Function
A function is a set of statements that completes a function that is defined by a keyword function + function name + a set of parameters;
Functions can be called repeatedly after they are defined, often written as a function, using functions to make the code more organized and more clearly structured.
Basic syntax: function Funname (arg0, arg1, ... argN) {//statements} (the function, function name, (), {}) is necessary for the constituent functions. () There can be no parameters. )
Example 1:function Say_hello (name, msg) {
Alert ("Hello" + name + ":" + msg);
} Say_hello ("David", "How is you today?");
Example 2: Returning a value from a function using the return statement: function sum (NUM1, num2) {return num1 + num2;} var s = SUM (1, 2); alert (s);
Note: 1. Any code that is behind the return statement will never be executed!
2. In JavaScript, you don't have to specify a return value!
3. When defining the parameters of a function, if the function is called without passing arguments, it does not give an error, but it can be judged in the function whether there is a pass-in argument, for example:
if (variable = = ' undefined ' | | variable = = NULL) {variable= ' 1 ';//You can give him a default value}
(If the function executes code with only one parameter, then only the first argument is used, even if there are two arguments.) )
Scope of the variable
A variable can be either global or local.
Global variables: can be referenced anywhere in the script, once you declare a global variable in a script, you can reference it anywhere in the script (including inside the function), the scope of the global variable is the entire script;
Local variable: only exists inside the function that declares it, cannot use it outside the function, the scope of the local variable is limited to the inside of the function, (so it can be used as a local variable)
Example: function square (num) {
var total; Total = num * num; return total;
} var total = 50; Alert (total);
var number = square (20); Alert (total);
var global = "global";//global variable in the function external to the VAR declared variable.
function Test () {
var local= "local"; A local variable is defined in a function, and a local variable is declared with Var.
Global2= "Global2"; Global variables are defined in the function, but are not declared with Var as global variables.
}
Internal functions of JavaScript
Some of the features in JavaScript are very common, and they are provided to the user as global functions, which are called intrinsic functions.
Eval ()
Eval takes a string-type argument, executes the string as code in the context, and returns the result of the execution;
Example: var i = 1; eval ("i = i + 1"); Eval ("alert (i);"); (meaning the ability to parse the code in a string)
Parseint and parsefloat Convert a string to a number
Cases:
var t3=parseint ("10.0");//==>10
var f3=parsefloat ("10.0");//==>10
var t1=parseint ("i = 4 + 1");//==>nan
var f1=parsefloat ("i = 4 + 1");//==>nan
var t2=parseint ("1.0555 = i-6");//==>1
var f2=parsefloat ("1.0555 = i-6");//==>1.0555
Escape and Unescape
URL encoding and decoding
Example: var url = "Http://www.baidu.com/s?name= millet"; url = Escape (URL); alert (URL); Alert (unescape (URL));
Utf-8 Code: Multi-language code GB2312/GBK: English
Object
JavaScript is an object-oriented language, so JavaScript programming can be done using object-oriented thinking
An object is a data entity that is composed of some properties and methods that are related to each other. (Is the property of the function = method)
Local objects (which are built-in objects at the beginning)
The 1.Date Date object is used to process the date and time.
var mydate = new Date (); Mydate.getfullyear (); Get the full year (4-bit, 1970-????) (note there's a new
At this stage, I understand that it means that a new object is put into the mydate, and if there is no new, it is equivalent to putting the value of date to mydate, not to the object.
Mydate.getmonth (); Get the current month (0-11, 0 for January)
Mydate.getdate (); Get current day (1-31)
Mydate.getday (); Get Current week x (0-6, 0 for Sunday)
Mydate.gettime (); Gets the current time (the number of milliseconds since 1970.1.1)
Mydate.gethours (); Get current number of hours (0-23)
Mydate.getminutes (); Gets the current number of minutes (0-59)
Mydate.getseconds (); Gets the current number of seconds (0-59)
Mydate.getmilliseconds (); Gets the current number of milliseconds (0-999)
Mydate.tolocaledatestring (); Get Current date
var mytime=mydate.tolocaletimestring (); Get current time
Mydate.tolocalestring (); Get Date and time
2.Math objects
The Math object is used to handle complex mathematical operations.
The Math object is a global object of JavaScript and does not need to be created with new
Math.Abs (-2); The method can return the absolute value of a number.
Math.Round (5.5); This method rounds a number to the nearest integer.
Math.random (); The method can return a random number between 0 (inclusive) ~ 1 (not included).
Math.ceil (1.4); The Ceil () method returns the smallest integer greater than or equal to X. (Take the big whole)
Math.floor (1.6); The floor () method returns the largest integer less than or equal to X. (Take small whole)
Math.pow (4,3); The POW () method returns the Y power of X. (4 of the 3-second party)
3.Array Array Object
3 ways to define an array
var week = new Array ();
var week = new Array (' Monday ', ' Tuesday, ' Wednesday ');
Var week =[' Monday ', ' Tuesday, ' Wednesday '];
Common methods: (The following methods also apply to strings because strings are special arrays)
Gets the length of the array. Length:alert (Week.length);
Gets the value corresponding to the array subscript: alert (week[0]);
Array element Additions:
Adds one or more new elements to the end of the array and returns the new length of the array
var Week_len = Week.push (' Thursday ', ' Friday ');
Adds one or more new elements to the beginning of the array, the elements in the array are automatically moved back, returning the new length of the array
var Week_len = week.unshift (' Sunday ');
Inserts one or more new elements into the specified position of the array, the elements of the insertion position are automatically moved back, and the "" is returned.
Arr3.splice (0,0, "abc");
document.write (Arr3[0]);
Array element deletion:
var del = ["AA", 23,345,56,34, "BB"];
var del_last = Del.pop (); Removes the last element and returns the element value
var del_first = Del.shift (); Removes the first element and returns the element value, and the elements in the array are automatically moved forward
var Del_arr = Del.splice (0,2); Removes the specified number of DeleteCount elements from the specified position deletepos, returning the removed element as an array
document.write (Del_arr);
That's it for today.
About Javascipt Basics 4