ES5-01创建对象、数据类型

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>数据类型</title>
</head>
<body>
<!-- 基本类型 :字符串 string  数字 NUMBER  布尔  undefined Symbol
引用: 数组(array) 对象(object) 函数(function) -->

    <script>
        //基础类型
       var str = '123' ;
       var num = 1;
       var bol =true; //false
       var km = null;
       var sym = Symbol('name');

       //引用类型
       var arr= [];
       var obj = {};
       var fun = function(){

       }

       var person1= {
           name:'kll'
       };
       person1.age='20'

       var person2= {
           name:'ggg'
       };

       /*
       person1 = person2
       console.log(person1,person2) // 俩个对象的属性和值都为{name:'ggg',age:'20'}
       person2.age='18'
       console.log(person1,person2)// 俩个对象的属性和值都为{name:'ggg',age:'18'}
       */

       var a = 10;
       var b = a;
       a++;
       console.log(a); //11
       console.log(b); //10
       
       var a1 = {};
       var b1 = a1;
       var b2 = a1;

       a1.name = 'song';
       console.log(a1); //{name: "song"}
       console.log(b1); //{name: "song"}

       b1.age = '30';

    //    const kk= {name:'23'};
    //    kk.color='345';
    const name1='laney';


    var s1 = new String('123');
    var obj1= new Object();
    var obj1= new Array();
    var s2 = String('123');
    var str2 = '123' ;
    
    // 引用和基本区别:

    // 1. 值是否可变,基本数据类型的值是不可变 ,但是引用类型的值可以变

    var str3 = 'abc' ;
    str3[0] = 'A';
    str3.toLocaleUpperCase();
    console.log(str3);
    
    str3 = 'mnkk';


    var person1= {
           name:'kll'
       };
     person1.age='20';
    //  person1 = {};

    // 2. 比较, 是否2个值相等
    //基础类型的比较是值的比较
    //引用数据类型是引用的比较
       var a=1;
       var b = true;

       console.log(a==b) //true
       var p1= {};
       var p2 ={};
       p1 === p2  //false
       
       //3. 值存放的位置
       // 基础数据类型存放在栈区, 引用类型的值同时保存在栈和堆内存中的对象

       //4 .复制的情况
       var m1= 10;
       var m2 = m1;
    //    m1和m2没有关系

     var b1= {};  //b1保存了一个 空的对象
     var c1= b1; 


     var arr1= [10,20,30];
     function add(){
        arr1[0] = 99;
     }
     add();
     console.log(arr1); //[99,20,30]

     
     var num =50;
     function add2(){
        var num = 99;
        console.log(num) //99
     }
     add2();
     console.log(num); // 50

    //  var num =50;
    //  function add2(){
    //     num = 99; //不加var 改全局
    //     console.log(num) //99
    //  }
    //  add2();
    //  console.log(num); // 99
     

     //申明变量类型
     var car= new String();
     var num = new Number();
     var arr0 = new Array();
     var obj1= new Object();

    //  基础数据类型检测 ; typeof
    typeof 2;    // number
    typeof '123';    //string
    typeof {};      //object
    typeof [];      //object
    typeof true;    //boolen
    typeof (function(){}); //function 

    
    Object.prototype.toString.call({})
    Object.prototype.toString.call([])
    Object.prototype.toString.call('123')
    Object.prototype.toString.call(2)
    Object.prototype.toString.call(true)
    Object.prototype.toString.call(null)
    Object.prototype.toString.call((function(){}))


    //对象的创建
    //object类型构造函数

    var  obj01= new Object();
    var  arr01= new Array();
    obj01.name ='夏明';
    obj01.sleep = function(){
        console.log(this.name+'在睡觉');
    }
    obj01.sleep()//夏明在睡觉

    // 2. 字面量定义
    var obj2= {};
    obj2.name='小明';
    // obj2['name'] = '小明';

    // var obj3 = {
    //     name:'wewe',
    //     age:'20',
    //     action:function(){}
    // };
    // var obj4 = {
    //     name:'song',
    //     age:'2',
    //     action:function(){}
    // };

   //工厂方式的形式
    function createObj(name,age){
        var obj = new Object();
        obj.name= name;
        obj.age= age;
        obj.action = function(){
            console.log(obj.name);
        }
        return obj;
    }

var obj3 = createObj('wewe','20');
var obj4 = createObj('song','2');

//构造函数
function Person(name,age){
    this.name= name;
    this.age= age;
}

Person.prototype.action = function(){
    console.log(this.name);
}

var p1 = new Person('laney','30');
var p2 = new Person('夜半天明','16');

//Object.create
var p1 = {name:'echo'};
var p2 = Object.create(p1); // p2 {}

//过程式开发 -- 站在一个执行者的角度
//洗衣服

//面向对象开发的模式   --站在一个指挥者的角度,开发思想
// 1.找个对象
// 2.让去洗
// 3.检查


// 面向对象的特点

// 1。封装  
// 2. 继承 ,提高代码的复用性
// 3. 多态 









    





     









       


   </script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值