html-- front-end JavaScript basic content

A, JavaScript variables

  In JavaScript to create variables are often referred to as "declaration" variable.
  Using the var keyword to declare variables.
Note:
1, the variable must begin with a letter
2, the variable can begin with $ and _ symbols (though we do not recommend it)
3, variable names are case sensitive (y and Y are different variables)
4, and JavaScript statements JavaScript variables are case-sensitive.

Example:

<Script type = " text / JavaScript " > // global variable 
    name = ' Seven ' ; // defined function     function FUNC () {
         // local variable var Age = 18 is ; // global variable 
        Gender = " M " 
    } </ script>            

    

    

        

        

 

Two, JavaScript data type
data type is classified into JavaScript object types and primitive types:
1, primitive types:
  numeric (Number The)
  JavaScript only a digital type. Decimal point numbers can also be used without:
    var = 34.00 X1; // use decimal write
    var x2 = 34; // do not use to write decimal

  Number object-related attributes
    Number.MAX_VALUE maximum value
    minimum value Number.MIN_VALUE
    Number.NaN special non-numeric value
    Number.NEGATIVE_INFINITY negative infinity
    Number.POSITIVE_INFINITY positive infinity
    Number.toExponential () by exponential notation format numbers
    Number.toFixed () using the fixed-point notation format numbers
    Number.toLocaleString () format digital conversion costs string
    Number.toPrecision () format numbers valid bit
    Number.toString () to - digital converted to a string
    Number.valueOf () returns the original value

  string (string)
  string can be any text enclosed in quotation marks. You can use single or double quotes:
    var carname = "Volvo XC60"; // double quotes
    var carname = 'Volvo XC60'; // single quotes

  String object attributes related
    to String.charAt () Returns a string of n characters
    of String.charCodeAt () Returns a string code of n characters
    String.concat () connection string
    the String.fromCharCode () created from a character code - string
    String.indexOf () to retrieve a string
    String.lastIndexOf () to retrieve a string from back to front
    String.length string length
    String.localeCompare () with a specific local order to compare two strings
    String.match () match is found or a plurality of regular expressions
    String.replace () is a replacement match the regular expression substring
    String.search () retrieves the regular expression match substrings
    String.slice () extracting a substring
    the String.split () dividing the string into a string array
    of String.substr () extracting a substring
    string .substring () returns a string substring
    String.toLocaleLowerCase () to convert a string lowercase
    String.toLocaleUpperCase () to convert the string to uppercase
    String.toLowerCase () to convert the string to lowercase
    String.toString () Returns a string
    the String.toUpperCase () the string into uppercase
    String.valueOf () returns a string

  Boolean (Boolean)
  Boolean (logical) can have only two values: true or false.
    to true X = var;
    var Y = to false;

  Boolean object-related attributes
    Boolean.toString () converts the character string into a Boolean value
    Boolean.valueOf () Boolean Boolean object

  2, object types:
    an array (the Array)
      var = new new cars the Array ();
      cars [0] = "Saab";
      cars [. 1] = "Volvo";
      cars [2] = "the BMW";
    or
      var cars = new Array ( "Saab ", "Volvo", "BMW");

  Array对象相关属性
    Array.concat( ) 连接数组
    Array.join( ) 将数组元素连接起来以构建一个字符串
    Array.length 数组的大小
    Array.pop( ) 删除并返回数组的最后一个元素
    Array.push( ) 给数组添加元素
    Array.reverse( ) 颠倒数组中元素的顺序
    Array.shift( ) 将元素移出数组
    Array.slice( ) 返回数组的一部分
    Array.sort( ) 对数组元素进行排序
    Array.splice( ) 插入、删除或替换数组的元素
    Array.toLocaleString( ) 把数组转换成局部字符串
    Array.toString( ) 将数组转换成一个字符串
    Array.unshift( ) 在数组头部插入一个元素

  对象(Object)
   对象由花括号分隔。在括号内部,对象的属性以名称和值对的形式 (name : value) 来定义。属性由逗号分隔:
    var person={firstname:"John", lastname:"Doe", id:5566};
    或者
    var person={
    firstname : "John",
    lastname : "Doe",
    id : 5566
    };

  Object对象相关属性
    Object.constructor 对象的构造函数
    Object.hasOwnProperty( ) 检查属性是否被继承
    Object.isPrototypeOf( ) 一个对象是否是另一个对象的原型
    Object.propertyIsEnumerable( ) 是否可以通过for/in循环看到属性
    Object.toLocaleString( ) 返回对象的本地字符串表示
    Object.toString( ) 定义一个对象的字符串表示
    Object.valueOf( ) 指定对象的原始值

  空(null)
    null是JavaScript语言的关键字,它表示一个特殊值,常用来描述“空值”。

  未定义(Undefined)
    undfined是一个特殊值,表示变量未定义。

三、JavaScript函数

  3.1、函数语法:
  函数就是包裹在花括号中的代码块,前面使用了关键词 function:
    function 函数名(){
      执行代码
    }

  3.2、函数的分类

// 普通函数
function func() {
    return "yusheng_liang"
}
// 匿名函数
var func = function(arg){
    return "yusheng_liang";
};
// 自执行函数
(function(arg){
    console.log(arg);
})('123')

3.3、调用带参数的函数

  在调用函数时,您可以向其传递值,这些值被称为参数。这些参数可以在函数中使用。您可以发送任意多的参数,由逗号 (,) 分隔:
  语法:
    function myFunction(var1,var2){
      代码
    }

  示例:

<button onclick="myFunction('Harry Potter','Wizard')">Try it</button>

<script>
function myFunction(name,job)
{
    alert("Welcome " + name + ", the " + job);
}
</script>

 

3.4、带有返回值的函数
  有时,我们会希望函数将值返回调用它的地方。通过使用 return 语句就可以实现。在使用 return 语句时,函数会停止执行,并返回指定的值。
  语法:
    function myFunction(){
      var x=5;
      return x;
    }


Guess you like

Origin www.cnblogs.com/june-L/p/11886455.html