这里写目录标题
- 1.引用类型
- 2.typeof判断类型
- 3.不同类型之前是否相等
- 4.类型转换
- json转params
- json变成query
- html 转 文字
- 获取地址的参数
- params转obj
- 5.一般大写的函数都是构造函数
- 6.构造函数
- encodeURI-编码
- encodeURIComponent-编码
- decodeURI-解码
- decodeURIComponent-解码
- -----------------Q&&A----------------------
1.引用类型
var a=100;//值类型
var b=a;
a=200;
console.log(b);//200
引用类型(对象 数组 函数) 对象只有一个 内存公用属性
var a={c:100};//a指针指向这个对象
var b=a;//b指针指向这个对象
a.c=200;
console.log(b.c);//200
所有引用对象都有__proto__属性
所有引用类型的隐式__proto__属性指向构造函数的Prototype属性
obj.proto==Object.prototype;
2.typeof判断类型
undefine underfine
‘abc’ string
123 number
true boolean
{ } object
[] object
null object
console.log function 函数是特殊的引用类型 其余的都是object
3.不同类型之前是否相等
100==‘100’;
0==‘’;//都转换成false
null==underfine;//转化成false
4.类型转换
JSON.stringify({a:10,b:20});把json变成字符串
JSON.parse(‘{a:10,b:20}’);把字符串变成json
json转params
function json2Param(json) {
if (!json) return ''
return cleanArray(
Object.keys(json).map(key => {
if (json[key] === undefined) return ''
return encodeURIComponent(key) + '=' + encodeURIComponent(json[key])
})
).join('&')
}
例
json2Param({name:'wjg',age:18})
"name=wjg&age=18"
json变成query
var format = function (val) {
val = val == null ? '' : val.toString().trim();
return encodeURIComponent(val);
}
function jsonToQuery(json) {
var query = [];
if (typeof json == 'object') {
for (var k in json) {
if (k === '$nullName') {
query = query.concat(json[k]);
continue;
}
if (json[k] instanceof Array) {
for (var i = 0, len = json[k].length; i < len; i++) {
query.push(k + '=' + format(json[k][i]));
}
} else {
if (typeof json[k] != 'function') {
query.push(k + '=' +format(json[k]));
}
}
}
}
if (query.length) {
return query.join('&');
} else {
return '';
}
}
例子:
var json = { id: 1, name: 'benny' };
jsonToQuery(json, true)
"id=1&name=benny"
html 转 文字
function html2Text(val) {
const div = document.createElement('div')
div.innerHTML = val
return div.textContent || div.innerText
}
html2Text('<div>111111</div>')
"111111"
html2Text('<div>123</div>')
"123"
获取地址的参数
params转obj
export function getQueryObject(url) {
url = url == null ? window.location.href : url
const search = url.substring(url.lastIndexOf('?') + 1)
const obj = {}
const reg = /([^?&=]+)=([^?&=]*)/g
search.replace(reg, (rs, $1, $2) => {
const name = decodeURIComponent($1)
let val = decodeURIComponent($2)
val = String(val)
obj[name] = val
return rs
})
return obj
}
eg:
getQueryObject('https://www.baidu.com/s?wd=%E9%98%BF%E8%90%A8&ie=utf-8&tn=68018901_7_oem_dg')
{wd: "阿萨", ie: "utf-8", tn: "68018901_7_oem_dg"}
getQueryObject('adasdsa?name=wang&age=18')
{name: "wang", age: "18"}
function param2Obj(url) {
const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
if (!search) {
return {}
}
const obj = {}
const searchArr = search.split('&')
searchArr.forEach(v => {
const index = v.indexOf('=')
if (index !== -1) {
const name = v.substring(0, index)
const val = v.substring(index + 1, v.length)
obj[name] = val
}
})
return obj
}
param2Obj('https://www.google.com/search?q=%E8%AF%95%E8%AF%95')
{q: "试试"}
5.一般大写的函数都是构造函数
function Foo(name,age){
this.name=name;
this.age=age;
return this;//默认返回this 写不写 无所谓
}
var foo=new Foo(‘张三’,18);
6.构造函数
var a={} 是new Object()的语法糖
var a=[]是new Array()的语法糖
function Foo()是new Function的语法糖
encodeURI-编码
encodeURI() 函数可把字符串作为 URI 进行编码;
encodeURIComponent-编码
encodeURIComponent() URI 组件中含有分隔符,比如 ? 和 #,/?=+$#,则应当使用;它会转化成%2F,%3A这种,都是由一个或多个十六进制的转义序列替换的。
decodeURI-解码
decodeURI() 对 encodeURI() 函数可把字符串作为 URI 进行编码;
decodeURIComponent-解码
decodeURIComponent() 函数可对 encodeURIComponent() 函数编码的 URI 进行解码。
一般带汉字的地址上需要通过这个来解码
-----------------Q&&A----------------------
js的数据类型:
数字(number)
字符串(string)
布尔值(boolean)
null
undefined
特殊对象–数组(array)
特殊对象–函数(function)
object类型
数据类型,判断方法
instanceof typeof Object.prototype.toString.call() //最准确最常用
Object.prototype.toString.call('') ; // [object String]
Object.prototype.toString.call(1) ; // [object Number]
Object.prototype.toString.call(true) ; // [object Boolean]
Object.prototype.toString.call(undefined) ; // [object Undefined]
Object.prototype.toString.call(null) ; // [object Null]
Object.prototype.toString.call(new Function()) ; // [object Function]
Object.prototype.toString.call(new Date()) ; // [object Date]
Object.prototype.toString.call([]) ; // [object Array]
Object.prototype.toString.call(new RegExp()) ; // [object RegExp]
Object.prototype.toString.call(new Error()) ; // [object Error]
null和undefined的区别
null表示没有对象,即该处不应该有值
1) 作为函数的参数,表示该函数的参数不是对象
2) 作为对象原型链的终点
undefined表示缺少值,即此处应该有值,但没有定义
1)定义了形参,没有传实参,显示undefined
2)对象属性名不存在时,显示undefined
3)函数没有写返回值,即没有写return,拿到的是undefined
4)写了return,但没有赋值,拿到的是undefined
null和undefined转换成number数据类型
null 默认转成 0
undefined 默认转成 NaN
字符串转number的方法
parseInt(num); // 默认方式 (没有基数)
parseInt(num, 10); // parseInt 使用基数 (十进制)
parseFloat(num) // 浮点型
Number(num); // Number 构造函数
~~num // 按位取反
num / 1 // 被 1 除
num * 1 // 被 1 乘
num - 0 // 减 0
+num // 一元操作 “+”
什么叫复杂类型?Object是引用类型,是引用传递,number,string是基本类型,是值传递?为什么会有这个区别?我把一个对象传到里面去?