Blogger Information
Blog 34
fans 0
comment 0
visits 26606
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
javacsript对象创建
罗盼的博客
Original
679 people have browsed it

实例

<!DOCTYPE HTML>
<html>
<head>
	
	<title>js创建对象的方式</title>
    
	  <script>
    //基于基础对象创建
    let obj = new object();  
    let obj = {};
    obj.name='小花'
    obj.sex='女'
    obj.date =1992
    obj.getInfo=function (){return 2018-this.date};
    console.log(obj.name);
    console.log(obj.sex);
    console.log(obj.getInfo());

    //构造函数
    function Student(name,age,sex){
        
       this.name= name;
       this.age=age;
       this.sex=sex;
    }
    Student.prototype.getinfo=function (){
        return this.name;
    }

    s1 = new Student('小花','18','男');    
    console.log(s1.getinfo());
     </script> 
</head>

<body>


</body>

</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

感悟:javascript对象的创建有三个方法:1.let  obj={}创建一个空对象。2.let obj=new object();基于原始对象创建,这样会继承原始对象的方法。3.通过构造方法创建

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments