-
js基础
-
JS输出
document.write():该语句仅仅是向页面输出写的内容,如果页面已经加载完毕,此时使用该语句会导致页面被覆盖;
//1、下面的代码不会覆盖整个页面
<body>
<p>这是我的测试段落</p>
<script>
document.write(Math.random())
</script>
</body>
//2、下面的代码会覆盖整个页面
<body>
<p>这是我的测试段落</p>
<button onclick="clickMe()"></botton>
<script>
function clickMe(){
document.write(Math.random())
}
</script>
</body>
-
JS的数据类型
-
9种数据类型
- Boolean
- Symbol(ES6新增类型)
- null
- undefiend
- String
- Number
- Array
- Object
- Function
-
检测数据类型
用来判断一个构造函数的prototype属性所指向的对象是否存在另外一个要检测对象的原型链上
- typeof操作符
typeof "John" ; // 返回 string typeof 3.14 ; // 返回 number typeof NaN ; // 返回 number typeof false ; // 返回 boolean typeof [1,2,3,4] ; // 返回 object typeof {name:'John', age:34}; // 返回 object typeof new Date(); // 返回 object typeof function () {}; // 返回 function typeof myCar; // 返回 undefined (如果 myCar 没有声明) typeof null; // 返回 object
//简单示例 <script type="text/javascript"> var a = function test (){ }; var b = {a:1}; var c = new String('123'); var d = new Number(2); var e = new Array([1,2]); console.log(c instanceof String); //true console.log('2' instanceof String); //false console.log(d instanceof Number); //true console.log(2 instanceof Number); //false console.log(e instanceof Array); //true console.log([1,2] instanceof Array);//true console.log(a instanceof Function); //true console.log(b instanceof Object); //true </script>
- instanceof运算符
- 用来检测 constructor.prototype 是否存在于目标参数的原型链上;
obj instanceof Object;//true 实例obj在不在Object构造函数中
-
继承中判断实例是否属于它的父类
function Person(){}; function Child(){}; var p = new Person(); Child.prototype = p;//继承原型 var c = new Child(); console.log(c instanceof Child);//true console.log(c instanceof Person);//true
- 用来检测 constructor.prototype 是否存在于目标参数的原型链上;
- constructor
- constructor 属性返回所有 JavaScript 变量的构造函数
"John".constructor // 返回函数 String() { [native code] } (3.14).constructor // 返回函数 Number() { [native code] } false.constructor // 返回函数 Boolean() { [native code] } [1,2,3,4].constructor // 返回函数 Array() { [native code] } {name:'John', age:34}.constructor // 返回函数 Object() { [native code] } new Date().constructor // 返回函数 Date() { [native code] } function () {}.constructor // 返回函数 Function(){ [native code] } //注意:null和undefined //Uncaught TypeError: Cannot read property 'constructor' of null //Uncaught TypeError: Cannot read property 'constructor' of undefined
- 使用示例
- 检测对象是否为日期
var myDate = new Date(); function isDate(date) { return date.constructor.toString().indexOf("Date") > -1; } var isD = isDate(myDate); // true
- 检测对象是否为数组
var fruits = ["Banana", "Orange", "Apple", "Mango"]; function isArray(myArray) { return myArray.constructor.toString().indexOf("Array") > -1; } var test = isArray(fruits); //true
- 检测对象是否为日期
- constructor 属性返回所有 JavaScript 变量的构造函数
-
undefined和null
-
null和undefined的定义
- null:主动释放一个变量引用的对象,表示一个变量不再指向任何对象地址;
- undefined:是所有没有赋值变量的默认值,自动赋值;
-
null和undefined的区别
- null和undefined的值相等,但类型不等
- 都是原始类型,保存在栈中变量本地
- undefined——表示变量声明过但并未赋过值,它是所有未赋值变量默认值
- null——表示一个变量将来可能指向一个对象,一般用于主动释放指向对象的引用
typeof undefined // undefined
typeof null // object
null === undefined // false
null == undefined // true