JavaScript基础知识

值类型   引用类型 

基本数据类型有   number string  boolean  null undefined   

typeof   可以得到   number  string   boolean    undefined    object    function

typeof  null  = object

== 与  === 的区别     

只有在  查看一个对象的属性是否存在时  如var obj={}   if(obj.a==null)   

或者 看一个函数的参数    function(a,b){ if(a==null)}  时 使用==

 

转换成false的情况     0 NaN    空字符串   null   undefined    false  

 

二:原型链 

四个问题  

如何判断一个变量是否是数组类型

写一个原型链继承的例子

描述new一个对象的过程

其他源码中如何使用原型链 

//构造函数   原型规则  原型链  instanceof

function Foo(name,age){       //构造函数    首字母大写

 this.name=name;

 this.age=age;

 this.class='class-1';

//return this;   //默认有这一行   

}

var f=new Foo('zhangsan',20)

 

var  a={}         var a=new Object()

var a=[]    var a=new Array()

function Foo(){}        var  Foo=new Function()

用instanceof判断一个函数是否是一个变量的构造函数

//原型规则

1.引用类型(对象,数组,函数)  都具有对象特性,可以自由扩展属性(除了null)

var obj={};   obj.a=100;

var arr=[];    arr.a=100;

function fn(){};

fn.a=100;

2.引用类型都有 __proto__属性, 属性值是一个普通的对象       隐式原型

console.log(obj.__proto__);

console.log(arr.__proto__);

console.log(fn.__proto__);

3.所有函数都有prototype属性,属性值是一个普通的对象     显式原型

console.log(fn.prototype)

4.引用类型的 __proto__属性指向它的构造函数的  prototype 属性 

console.log(obj.__proto__===Object.prototype)

5.当试图得到一个对象的属性时,如果这个对象本身没有这个属性,则回去的它的__proto__(构造函数的prototype)中寻找

function Foo(name,age){

  this.name=name;

}

Foo.prototype.alertName=function(){

  alert(this.name)

}

var f=new Foo('zhangsan');

f.printName()=function(){

  console.log(this.name);

}

f.printName();

f.alertName();

f.tostring()    //要去 f.__proto__.__proto__中去找   即Object

//简单的例子

function Animal(){

  this.eat=function(){

    console.log('animal eat');

}

}

function Dog(){

  this.bark=function(){

    console.log('dog bark');

}

}

Dog.prototype=new Animal();

function Hashiqi(){

  this.play=function(){

    alert("hashiqi play");

}

}

Hashiqi.prototype=new Dog();

var myerha=new Hashiqi();

 

关于原型 和原型链   还不太透彻

https://www.jianshu.com/p/a4e1e7b6f4f8

posted @ 2020-09-19 19:36  YITTT  阅读(98)  评论(0)    收藏  举报