js变量

来源:互联网 发布:淘宝ccr手表怎么样 编辑:程序博客网 时间:2024/05/17 09:03

基本类型:4 ‘str’ true/false undefined null
引用类型:[] {}
基本类型和引用类型区别基本类型的值不可修改,
引用类型的值可以修改

    4->3    var num = 4;        num = 3; 

这里发生的是覆盖 下面更加形象的例子

var str = 'string';var anotherStr = str.replace('s', '');console.log(str + ' ' + anotherStr);

返回的结果是 string tring
返回的是一个新的字符串而没有在原来的字符串上面修改,而且所有字符串的方法对字符串操作,都不会修改原来字符串值,而是返回一个新的字符串。

    var person = {};    console.log(person);    person.name = 'Xiao Ming';    person.sex = 'Male';    person.sex = 'Female';    person.family = ['Ming Ba', 'Ming Ma'];    delete person.sex;    console.log(person);

这里引用类型的值是可以修改的 ,引用类型可以添加属性,修改属性,删除属性。
基本类型之所有有属性和方法

 'string'.replace 1->Number 'str'->String

基本类型调用方法的时候找到对应的包装对象。
基本类型本身没有属性和方法,而是借用包装对象上的属性和方法。

堆 栈
相等比较
栈空间大小固定,空间大小不固定

    var xmScore = 4;    var xhScore = 4;    console.log(xmScore === xhScore);

基本类型比较相等即相等

    var xm = {        age: 18,        score: 4    };    var xh = {        age: 18,        score: 4    };    console.log(xm===xh);

这里返回的值是false 因为两个对象在堆内存中开辟的是两个内存空间。

    var xm = {        age: 18,        score: 4    };    var xh = xm;    console.log(xm === xh);

这里返回的数值是true,因为两个变量是同一个引用,指向同一个堆内存空间。

    function equalObjs(a, b) {        for (var p in a) {            if (a[p] !== b[p]) return false;        }        return true;    }    console.log(equalObjs(xm, xh));

上面进行判断两个对象数值是不是相等。这里进行的是浅拷贝。

    function equalArrays(a, b) {        if (a.length !== b.length) return false;        for (var i = 0; i < a.length; i++) {            if (a[i] !== b[i]) return false;        }        return true;    }

判断两个数组的数值是不是一样。

复制变量的值,分基本类型和引用类型

    var xmScore = 4;    var xhScore = xmScore;    xhScore++;    console.log(xhScore);    console.log(xmScore);

基本类型的值复制互不干涉

var xm = {        age: 18,        score: 4    };    var xh = xm;    xh.score++;    console.log(xh.score);    console.log(xm.score);

他们的是同一个引用,所以结果都是5。

    var xm = {        age: 18,        score: 4    };    function copyObj(obj) {        var newObj = {};        for (var p in obj) {            newObj[p] = obj[p];        }        return newObj;    }    xh = copyObj(xm);    console.log(xh === xm);

返回的值是false 是浅拷贝

参数的传递

function addTen(num) {        return num + 10;    }    var score = 10;    console.log(addTen(score));    num = score;

基本类型参数传递

    function setName(obj) {        return obj.name = 'xm';    }    var person = {};    setName(person);    console.log(person.name);    obj = person

引用类型也是按值传递,这传递setName(person); person 传递的是地址 地址是值 。

    function setName(obj) {        obj.name = 'xm';        obj = {};        obj.name = 'xh';    }    var person = {};    setName(person);    console.log(person.name);    obj = person

检测类型
4
‘string’
true
undefined
null
[]
{}
function
RegExp

console.log(typeof(4));    console.log(typeof 'str');    console.log(typeof true);    console.log(typeof undefined);    console.log(typeof null);    console.log(typeof []);    console.log(typeof {});    console.log(typeof function () {});    console.log(typeof /a/);

typeof 既可以当运算符使用,可以当作函数使用。

instanceof

    console.log([] instanceof Array);    console.log([] instanceof Object);    console.log({} instanceof Object);    console.log({} instanceof Array);    console.log(1 instanceof Number);
0 0
原创粉丝点击