首页 技术 正文
技术 2022年11月16日
0 收藏 573 点赞 3,979 浏览 1236 个字

1、对象的属性访问:

对象.属性

对象[属性],但中括号中必须是字符串

2、属性的遍历:

for in方法举例:

var ren={};

ren.name=”名字”;

ren.eat=function(){

alert(“吃饭”);

}

for(var i in ren){

alert(ren[i]);

}

3、封装-工厂函数,这种方式格式不规范

function dianshi(color,size,brand){

var tv={};

tv.color=color;

tv.size=size;

tv.brand=brand;

tv.play=function(){

alert(“玩”);

}

tv.look=function(){

alert(“看”);

}

return tv;

}

var d1=dianshi(“red”,”42cun”,”sony”);

var d2=dianshi(“blue”,”40cun”,”长虹”);

var d3=dianshi(“green”,”30cun”,”康佳”);

alert(d1.color);

alert(d2.size);

alert(d3.brand);

4、封装-构造函数,这种方式格式更正式

function tv(color,size,brand){

this.color=color;

this.size=size;

this.brand=brand;

this.play=function(){

alert(“玩”);

}

this.look=function(){

alert(“看”);

}

}

var d1=new tv(“red”,”42cun”,”sony”);

var d2=new tv(“blue”,”40cun”,”长虹”);

var d3=new tv(“green”,”30cun”,”康佳”);

alert(d1.color);

alert(d2.size);

alert(d3.brand);

5、封装-代码段

prototype方法:将共有的方法和属性添加到代码段中

function tv(color,size,brand){

this.color=color;

this.size=size;

this.brand=brand;

this.look=function(){

alert(“看”);

}

}

tv.prototype.play=function(){

alert(“玩”);

}

var d1=new tv();

d1.play();

6、混合方法:几种方式的混合,扬长避短

function tv(color,size,brand){

this.color=color;

this.size=size;

this.brand=brand;

this.look=function(){

alert(“看”);

}

}

tv.prototype.obj={name:”名字”}

tv.prototype.play=function(){

alert(“玩”);

}

var d1=new tv();

d1.play();

alert(d1.obj.name);

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,893
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,421
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,240
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,052
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,683
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,719