JavaScript笔记(基础)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript笔记(基础)相关的知识,希望对你有一定的参考价值。

1.
------------------------------------------------------------------------------------------
javascript can "display" data in different ways:

Writing into an alert box, using window.alert().
Writing into the html output using document.write().
Writing into an HTML element, using innerHTML.
Writing into the browser console, using console.log().

Using document.write() after an HTML document is fully loaded, will delete all existing HTML: The document.write() method should be used only for testing.

下面这种的就不会:
<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>
<p>My first paragraph.</p>

<script>
window.alert(5 + 6);
</script>

</body>
</html>

------------------------------------------------------------------------------------------
== equal to
=== equal value and equal type
!= not equal
!== not equal value or not equal type

var x = new String("John");
var y = new String("John");
// (x == y) is false because x and y are different objects
// (x == x) is true because both are the same object
------------------------------------------------------------------------------------------
typeof Returns the type of a variable
instanceof Returns true if an object is an instance of an object type

-----------------------------------------------------------------------------------------

var cars = ["Saab", "Volvo", "BMW"]; // Array
var x = {firstName:"John", lastName:"Doe"}; // Object

----------------------------------------------------------------------------------------
JavaScript evaluates expressions from left to right. Different sequences can produce different results:
var x = 16 + 4 + "Volvo";
Result: 20Volvo

----------------------------------------------------------------------------------------
array is an object too
typeof [1,2,3,4] // Returns object
typeof {name:‘John‘, age:34} // Returns object
----------------------------------------------------------------------------------------
var person; // Value is undefined, type is undefined
person = undefined; // Value is undefined, type is undefined
var person = null; // Value is null, but type is still an object
null === undefined // false
null == undefined // true
-------------------------------------------------------------------------------------------
调用函数不加括号,就是打印函数
function toCelsius() {
return (5/9) ;
}
document.getElementById("demo").innerHTML = toCelsius; //这个就是打印这个函数,而不是调用
------------------------------------------------------------------------------------------

js就是贱,没有class就可以有对象了。。。

<script>
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
document.getElementById("demo").innerHTML =
person.firstName + " is " + person.age + " years old.";
</script>
------------------------------------------------------------------------------------------

You can access object properties in two ways:

objectName.propertyName
or
objectName["propertyName"]
-----------------------------------------------------------------------------------------
Do Not Declare Strings, Numbers, and Booleans as Objects!
When a JavaScript variable is declared with the keyword "new", the variable is created as an object:
千万别这么干。。。
var x = new String(); // Declares x as a String object
var y = new Number(); // Declares y as a Number object
var z = new Boolean(); // Declares z as a Boolean object
但是可以直接当对象来用
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;
-------------------------------------------------------------------------------------------
//If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable.
// code here can use carName
function myFunction() {
carName = "Volvo";

// code here can use carName

}
--------------------------------------------------------------------------------------------
事件:

Event Description
onchange An HTML element has been changed
onclick The user clicks an HTML element
onmouseover The user moves the mouse over an HTML element
onmouseout The user moves the mouse away from an HTML element
onkeydown The user pushes a keyboard key
onload The browser has finished loading the page
---------------------------------------------------------------------------------------------
字符串:

String Properties
constructor Returns the function that created the String object‘s prototype
length Returns the length of a string
prototype Allows you to add properties and methods to an object

String Methods
charAt() Returns the character at the specified index (position)
charCodeAt() Returns the Unicode of the character at the specified index
concat() Joins two or more strings, and returns a copy of the joined strings
fromCharCode() Converts Unicode values to characters
indexOf() Returns the position of the first found occurrence of a specified value in a string
lastIndexOf() Returns the position of the last found occurrence of a specified value in a string
localeCompare() Compares two strings in the current locale
match() Searches a string for a match against a regular expression, and returns the matches
replace() Searches a string for a value and returns a new string with the value replaced
search() Searches a string for a value and returns the position of the match
slice() Extracts a part of a string and returns a new string
split() Splits a string into an array of substrings
substr() Extracts a part of a string from a start position through a number of characters
substring() Extracts a part of a string between two specified positions
toLocaleLowerCase() Converts a string to lowercase letters, according to the host‘s locale
toLocaleUpperCase() Converts a string to uppercase letters, according to the host‘s locale
toLowerCase() Converts a string to lowercase letters
toString() Returns the value of a String object
toUpperCase() Converts a string to uppercase letters
trim() Removes whitespace from both ends of a string
valueOf() Returns the primitive value of a String object
-----------------------------------------------------------------------------------------------
数字:


Property Description
MAX_VALUE Returns the largest number possible in JavaScript
MIN_VALUE Returns the smallest number possible in JavaScript
NEGATIVE_INFINITY Represents negative infinity (returned on overflow)
NaN Represents a "Not-a-Number" value
POSITIVE_INFINITY Represents infinity (returned on overflow)
----------------------------------------------------------------------------------------------

以上是关于JavaScript笔记(基础)的主要内容,如果未能解决你的问题,请参考以下文章

JavaScrip笔记心得(持续更新)

web前端 --- JavaScrip基础

JavaScrip基础

JavaScrip的入门基础1

javascrip菜鸟

JavaScrip和Java一样吗?