码迷,mamicode.com
首页 > 其他好文 > 详细

6.2.4 Object_构造函数 对象

时间:2018-07-21 12:07:26      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:点语法   val   成员   var   ons   win   type   方法   添加   

一. 对象的创建

1. 字面量方式创建   #推荐使用这种方式

 key:value

var stu = {

  name:‘alex‘,

  age:22

}

console.log(stu)

console.log(window)

//点语法 包括get方法和set方法

var n = stu.name

console.log(n)

 

stu.age=44;

console.log(stu.age)

 

2. 使用Object()创建对象

function add(){

}

add()

Object() 构造函数 

1. 首字母大写 碰见构造函数 要想创建对象new

2. 构造函数不需要写return

3. 为对象添加成员变量:this.name = ‘alex‘

var obj = new Object();

obj.name=‘xxx‘

console.log(obj)

 

var stu=function(){

  this.name = ‘xx‘;

  this.age=14;

  this.fav=function(){

    console.log(‘泡xx‘)

  }

}

var s = new stu();

console.log(s)

 

推荐大家使用的构造函数的方式

function Animal(){

  this.name = ‘xx‘;

  this.age=12

//  this.fav = function(){

//    console.log(this.age)

//  }

}

 

Animal.prototype.showname=function(){

  alert(this.name)

  //执行相应的操作

}

 

Animal.prototype.showname2=function(){

  alert(this.name)

  //执行相应的操作

}

var a = new Animal()

a.showname()

 

6.2.4 Object_构造函数 对象

标签:点语法   val   成员   var   ons   win   type   方法   添加   

原文地址:https://www.cnblogs.com/beallaliu/p/9345641.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!