前端---Javascript对象

1.对象和关联数组

Js对象与纯粹的面向对象语言的对象存在一定的区别,Js对象本质上是一个关联数组,或者说更像Java的MaP数据结构,有一组Key-Value组成。与Java的区别是 Value不仅是值,还可以是函数的等。下面看例子

function person(name,age){
		this.name =name;
		this.age = age;
		this.info = function(){
			alert("姓名"+this.name+"年龄"+this.age);
		};
		}
var p ={
			walk:"abc",			
			foot:function(say){
				alert(say+'  '+this.walk);
			}
		}		

 2.创建对象

1》.使用new创建

2》.使用Object创建

3》.使用JSON创建

var a =new person('aa',20);
​

var p ={
			walk:"abc",			
			foot:function(say){
				alert(say+'  '+this.walk);
			}
		}		


​

 var myobj =new Object();

3.继承

js中的继承是伪继承是从新定义之后附加给父类的方式,主要使用prototype关键词

如下:

      function Person(name,age){
      	this.name =name;
      	this.age =age;      	
      }
       Person.prototype.sayHellow =function(){
       	console.log(this.name +"  Hellow");
       }
       var per =new Person("牛",22);
       per.sayHellow();

猜你喜欢

转载自blog.csdn.net/qq_33188563/article/details/81078985