JS Basic Six

Source: Internet
Author: User

Date objects represent dates
No base type, auto Create
You need to add new to create the object manually
var mydate = new Date ()
Console.log (MyDate) returns a string--a string of the current time


Method of using the date method constructor call itself

Setting the contents of the tag directly in the webpage recommends using the Mozla MDN on Firefox website, international website, can switch to Chinese, cannot be used offline
Document.vrite try not to use this method, relatively old some problems
Console.log not available in low version IE
var mydate = new Date () This is the current time

Console.log (Mydate.getdate ()); Gets the current numbered
Console.log (Mydate.getmonth ()); Gets the current number of months
Console.log (Mydate.getfullyear ()); Get current year
Console.log (Mydate.getday ()); Gets the current day of the week
Console.log (Mydate.gethours ()); Gets the current number of hours
Console.log (Mydate.getminutes ()); Gets the current number of minutes
Console.log (Mydate.getseconds ()); Gets the current number of seconds
Console.log (Mydate.getmilliseconds ()); Gets the current number of milliseconds IE cannot be accurate to 1 milliseconds, can get up to 10 milliseconds, 30 milliseconds, the error is unavoidable
Console.log (Mydate.gettime ()); Generate a time setback, avoid the same number, require a unique ID, produce the same number will produce an accident, generate the current time of the time
Console.log (Mydate.gettimezoneoffset ()); This is the number of minutes to get the local time, which is the difference between the local time and the standard time.
Console.log (Mydate.getutchours ()); Gets the number of hours of the current time of the standard Time


Mydate.sethouts (10); This is set to 10 points ahead of the current time
Console.log (Mydate.getutchours ())


ToString converts each object into a string,
var mydate = new Date ()

Console.log (Mydate.tostring ()); Here is the meaning of converting the current date to a string
Console.log (Mydate.tolocalestring ()); The format of the local customary time
Console.log (mydate.tolocalestring ()) print out in local format

Palindrome: ' 1234 ', ' 12344321 '
' abcd123 ', ' ABCD123321DCBA '


function fn (str) {
Convert a string to a string array
var arr = Str.split (");
Reverses the order of string arrays
var Reversearr = Arr.reverse ();
Concatenation of arrays into strings after inverted order
var reversestr = Reversearr.jojn (");
Stitch the original string together with the inverted string to get the result
Retrun str +reversestr;

The above proficiency can be abbreviated as follows: Return str + str.split ("). Reverse (). Join (';)
}
Console.log (FN (' 1234 '))
Console.log (FN (' abc '))


Enter the sentences in English and change the order of the words under ' This was an apple ' and ' Apple an was this '
function fn (str) {
Arrays that are separated into words
var arr = Str.split (");
var result = Arr.reverse (). Join (')
return result;
}


Console.log (FN (' This was an apple)

Detecting class names
function Checkclass (allclass,c) {
Get an array of class names
var listofclass = Allclass.split (")

Determines whether the value of C has occurred in the array.
The hypothesis does not appear (with: true, no: false)
var result = Flase;
for (var i = 0;i < listofclass.length;i++) {
if (listofclass[i] = = = c) {
If the two values are equal, the value of an item in the array is equal to C, and the previous assumption is wrong
result = true;
Break

}
return result;
}

The}//indexof method, if not found, returns 1, and if found, returns the angular label of the item in the array.

Method Two: if (Listofclass.indexof (c) = = = 1) {
return false;
} else {
return True
}


Console.log (Checkclass (' name My-box right ', ' right ')); True
Console.log (Checkclass (' name My-box right ', ' left ')); False
Console.log (Checkclass (' name My-box right ', ' box ')); False
Console.log (Checkclass (' name My-box right ', ' left ')); False


b does not change with a change
var a = 10;
var B = A;
A = 20;
Console.log (b); Print result is 10

Object: is a reference type, reference type: When the data of the reference type is assigned to the variable, the data itself is assigned to the variable, but the reference to the data is assigned to the variable "as the obj reference type, all objects are reference types, all types are called reference types a variable is modified, The other variable changes as well.
var obj1{1, save the object in another place, assign a value to Obj1, Obj1 is a variable,
A:10,
B:20
};
var obj1 = obj2; 2,
obj1.a = +; A is the meaning of the object, and 30 is assigned to the obj1.a.
Cosole.log (objb.2)

In the same function, a variable with the same name declared later overrides the previously declared variable

10.20
var obj1 = {
A:10,
B:20
};
var obj2 ={
A:10,
B:20
};
obj2.a = 30;
Console.log (obj1.a) print result is 10


Write a function that receives a string that returns the number of vowels that appear in the string (AEIOU)
For example: FN (' ABCDE ')//2
fn (' aabbcc ')//2
function fn (str) {
Affirms the number of vowels, with an initial value of 0
var count = 0;
for (var i = 0; var < str.length; i++) {
Determine if str[i] is a vowel and if so, Count plus 1
Method One,
if (str[i] = = = A | | str[i] = = = E | | str[i] = = = I | | str[i] = = = O | | str[i] = = = u) {
count++;
}


Method Two,
if ([' A ', ' e ', ' I ', ' O ', U]), IndexOf (Str[i])!==-1) {
count++;
}

Method Three,
Switch (Str[i]) {
Case ' a ':
Case ' E ':
Case ' I ':
Case ' O ':
Case ' U ':
count++;
}

}
return count;
}
Assigning a property does not require a declaration

# #浏览器模型
All created window created global variables will have the same properties in Windows
Create a local variable inside the function,

var abc = ' Hello ';
Console.log (WINDOW.ABC)

Sccreenx returns the x-coordinate relative to the screen window, IE does not support
Sccreeny returns the y-coordinate relative to the screen window IE browser does not support
Screenleft returns the coordinates relative to the window, that is, the distance from the browser window to the left of the screen is supported by all browsers
Screentop returns the coordinates relative to the window
In the browser console, enter an expression, and the variable can get the return value
Window.innerwith returns the width of the document display area of the window

Windows Mobile window can only be used in IE browser

ScrollBar control: Window.scrollby (0,50) The first parameter represents a horizontal scroll bar, and the second parameter represents the movement of the vertical scrollbar relative to the current position (as opposed to the current position
Window.scrollto (0,50) is positioned relative to the location of the browser window

History holds the object, and the attributes are: Length returns the record in the record list
History.back
History.forward the way the browser history progresses,
History.go (2) means that both forward and backward Method 2 represents the number of steps, if it is negative, it is backward


Location.href shows the current full URL
Location. Hostname the domain name of the current URL
Pathname the URL path name returned by
Port returns the port number used by the URL server
Search returns the query part of a URL
The question mark behind the URL is called

Screen Object
Screen.width returns the total width of the screen
Screen.height returns the total height of the screen
Avaiheight returns the height of the screen, which is supported in the height standard browser of the taskbar, is not supported in IE, and the next is
Screen.avaiwidth returns the width of the screen, including the width of the taskbar

Navigator Object
Navigator.appcodename return the code name of the browser
Navigator.appname returns the name of the browser
Navigator.appversion getting something related to the current browser
Platform operating system platform running the browser
UserAgent
Open Source: After the code is developed, other people are open source and need to follow the Open source protocol (for example, if you use my open source code, your code is open source, this is a more restrictive protocol
Khtml Browser kernel

Avoid using global variables, you can create variable anonymous functions inside a function: a function called once, can not name

Execute function immediately/also called self-executing function: A way to avoid global variables, put all the code in an immediate execution function, create a variable is not a global variable, is a variable created inside the function
(function () {
var a = 10;
var B = 20;
})()

Time interval Function! Important to
(function () {
var fn = function () {
Console.log (' Hello.world ')
};
fn Replace FN with this: SetTimeout (' Console.log (\ ' Hello,world ' \), 2000) two parameters, the first is the JS code to be executed, the second delay how many milliseconds
}) (); The first parameter above is normally passed to the function.
The first step evaluates the value of the argument,
If you do not write the return manually, the system will automatically return to the return undefined;

SetTimeout delay a period of time to execute a piece of code first calculate the argument setTimeout (function) {},)
SetTimeOut call a function or expression after a specified number of milliseconds
SetInterval to execute a code setinterval (function) {} at intervals)
Synchronous and Asynchronous ' synchronization: For a more time-consuming thing, wait for this thing to finish, then do the following things for prompt,alert,confirm these three statements are synchronous statements that block the JS code Next statement completion
Async: For a relatively time-consuming thing, while doing this, do the following: Settimeout,setinterval currently only these two are asynchronous, the others are synchronous
(function () {
function SayHello () {
Console.log (' Hello ');
}
SayHello ();

for (var i = 0; i < 10000; i++) {1 seconds equals 1000 milliseconds
var a = 10;
var B = A * B-15;
//}
Console.log (' Hello ')


SetTimeout (function ()) {
Console.log (' deferred execution ')
},2000;
Console Log (' Hello ')
})();

Clearinterval Stop


(function () {
Use the Times variable to represent the number of prints
var times = 0;

SetInterval (function () {
Console.log (1);
times++;

Console.log (' This is the first ' + Times + ' print ');
if (Times >= 20) {
Clearinterval (times);

}
},1000);
})();


(function () {
for (var i = 1;i <= 20;i++) {
SetTimeout (function (i) {
var i = The current value of this loop i
return function () {
Console.log (i);
}
},) (i) i*1000);
}
}();

JS Basic Six

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.