【JavaScript】基本类型和引用类型的值、引用类型

一、前言

       接着上一篇继续记笔记

二、内容

        动态的属性

var person = new Object();
person.name = "Nicholas";
alert(person.name);  //"Nicholas"

不能给基本类型的值添加属性
var name = "Nicholas";
name.age = 27;
alert(name.age); //undefined        

         传递参数

function setName(obj){
    obj.name = "Nicholas";

//以下为局部对象,毫无作用
obj = new Object();
obj.name = "Greg"; }

        没有块级作用域

if(true){
   var color = "blue";
}
alert(color);  //"blue"



for(var i=0;9<10;i++){
doSomething(i);
}
alert(i); //10

      Object类型

var person = {
    name : "Nicholas",
    age : 29
5 : true
};

alert(person["name"]);
alert(person.name)

     Array类型

var colors = new Array();
var colors = new Array(20);
var colors = new Array("red","blue","green");

var colors = ["red","blue","green"];
var colors = [];

//基本方法
Array.isArray(object); 是否为数组

//队列方法
array.push(“item1”,“item2”); 向数组末尾添加若干元素 并 返回修改后的数组长度
array.pop(); 从数组末尾移除最后一项 并 返回移除的项
array.shift(); 从数组取得第一项
array.unshift(“item1”,“item2”); 向数组的头部添加任意个项 并 返回新数组的长度

//重排序方法
array.reverse(); 反转数组顺序
array.sort(); 排序

//操作方法
array.concat("item1",["item2","item3"]); 连接其它元素 并 返回新数组
array.slice(startIndex,endIndex); 从数组中抽取数组
array.splice(startIndex,howmany,"item1","item2",..) 删除(前两个参数),插入(第2个参数设为0),替换

//位置方法
array.indexOf(element) //从数组头部开始找
array.lastIndexOf(element) //从数组尾部开始找

//迭代方法
array.every(function(item,index,array)) //每一项都返回true,则返回true
array.filter(function(item,index,array)) //返回符合function条件的数组
array.forEach(function(item,index,array)) //无返回值
array.map(function(item,index,array)) //返回每次函数调用的结果组成的数组
array.some(function(item,index,array)) //任一一项返回true,则返回true

//归并方法
array.reduce(function(prev.cur.index.array)) //从数组第一项迭代所有项 并 返回一个值
array.reduceRight(function(prev.cur.index.array)) //从数组最后一项迭代所有项 并 返回一个值

      Date类型

var now = new Date();

     RegExp类型

var expression = / pattern / flags;
var re = new RegExp(pattern, flag);

flag: g——全局模式,应用于所有字符串
i——不区分大小写
m——多行模式

var matches = regExp.exec(text) //返回包含第一个匹配信息的数组
matches.index —— 匹配项在字符串中的位置
matches.input —— 输入的字符串

regExp.test(text) —— 目标字符串与某个模式是否匹配

     Function类型

function c(propertyName){
    return function(object1,object2){
         var value1 = object1[propertyName];
         var value2 = object2[propertyName];

         if(value1 < value2){
              return -1;
         }else if (value1 > value2){
              return 1;
         }else{
              return 0;
         }
   }
}

//调用
var s = c("name");
var result = s(a,b);

//在特定的作用域中调用函数
obj.call(thisobj,arg1,arg2,...);
obj.apply(thisobj,[arg1,arg2,...]);
obj.bind(thisObj,arg1,atg2);

      Number类型

Number类型的实例化对象提供了
number.toFixed(n) —— 按指定的小数位返回数值的字符串表示
number.toExponential(n) —— 返回指数表示法,能指定小数位

     String类型

string.substring(startIndex, endIndex) —— 根据头尾位置返回一个字符串
string.substr(startIndex, length) —— 根据头位置与长度返回一个字符串
string.indexOf("s",index) —— 从字符串头部开始,返回某字符在字符串中的索引,第二个参数指定从哪个索引开始检索
string.lastIndexOf("s",index) —— 从字符串尾部开始,返回某字符在字符串中的索引,第二个参数指定从哪个索引开始检索

string.match(pattern) —— 字符串根据正则返回数组
string.search(pattern) —— 字符串根据正则返回第一个匹配项的索引
string.replace(oldstring.newstring) —— 只替换第一个符合的,如果替换全部则第一个参数需要使用正则

      Math对象

Math.min()
Math.max()
Math.ceil() —— 执行向上舍入
Math.floor() —— 执行向下舍入
Math.round() —— 执行标准舍入
Math.random() —— 方法返回大于等于0小于1的一个随机数
原文地址:https://www.cnblogs.com/lovecsharp094/p/8431368.html