js面向对象学习笔记之一(类与对象)

来源:互联网 发布:淘宝蚂蚁推门网上大学 编辑:程序博客网 时间:2024/05/16 10:58

1. javascript中的类即函数


/*==============================类即函数=====================================*/function People(name,sex,addr){this.name = name;this.sex = sex;this.addr = addr;this.foo = function(){alert(this.name + this.sex+this.addr);}}var people = new People('张三','男','浙江杭州');people.foo();  //张三男浙江杭州 var newp = new People('历史','男','浙江杭州');    /*这里不能使用对象代替函数名,且添加的函数属性是共有的*/People.prototype.getName = function(){alert(this.name);}    /*这里添加的函数属性 仅仅是当前对象私有的*/people.getName1 = function(){alert(this.name);}/**/people.getName();  //张三people.getName1(); //张三newp.getName();    //历史

2. javascript中的特殊对象

/*=============================自定义的对象 可以理解为单例模式================================*/    var Human = {name:'',sex:'',addr:'',createh:function(name,sex,addr){this.name = name;this.sex = sex;this.addr = addr;},foo : function(){alert(this.name + this.sex+this.addr);}};Human.createh('李梅','女','浙江杭州')Human.foo();  //李梅女浙江杭州



1 0
原创粉丝点击