第五课

第五课

一、面向对象
1、创建对象属性
  1. var website={};
  2. website.domainName="www.806501.com";
  3. website.name="806501";
  4. website.email="kavkill@806501.com";
  5. website.age=3;
  6. website.click=3000;
  7. console.log(website);
返回:
{ domainName: ‘www.806501.com’,
  name: ‘806501’,
  email: ‘kavkill@806501.com’,
  age: 3,
  click: 3000 }
2、创建对象方法
  1. var website={};
  2. website.domainName="www.806501.com";
  3. website.name="806501";
  4. website.email="kavkill@806501.com";
  5. website.age=3;
  6. website.click=3000;
  7. console.log(website);
  8. website.setDomainName=function(domainName){
  9. this.domainName=domainName;
  10. }
  11. website.getDomainName=function(){
  12. return this.domainName;
  13. }
  14. website.setDomainName("nodejs.806501.com");
  15. console.log(website.getDomainName());
  16. console.log(website);
返回:
{ domainName: ‘www.806501.com’,
  name: ‘806501’,
  email: ‘kavkill@806501.com’,
  age: 3,
  click: 3000 }
nodejs.806501.com
{ domainName: ‘nodejs.806501.com’,
  name: ‘806501’,
  email: ‘kavkill@806501.com’,
  age: 3,
  click: 3000,
  setDomainName: [Function],
  getDomainName: [Function] }
3、另一种方式创建属性和方法
  1. var website={
  2. domainName:"www.806501.com",
  3. name:"806501",
  4. email:"kavkill@806501.com",
  5. age:3,
  6. click:3000,
  7. setDomainName:function(dn){
  8. this.domainName=dn;
  9. },
  10. getDomainName:function(){
  11. return this.domainName;
  12. },
  13. };
  14. console.log(website);
  15. website.setDomainName("nodejs.806501.com");
  16. console.log(website.getDomainName());
返回:
{ domainName: ‘www.806501.com’,
  name: ‘806501’,
  email: ‘kavkill@806501.com’,
  age: 3,
  click: 3000,
  setDomainName: [Function],
  getDomainName: [Function] }
nodejs.806501.com
4、构造函数
  1. function WebSite(domainName,name,email,age,click){
  2. this.domainName=domainName;
  3. this.name=name;
  4. this.email=email;
  5. this.age=age;
  6. this.click=click;
  7. this.setDoaminName=function(domainName){
  8. this.domainName=domainName;
  9. }
  10. this.getDomainName=function(){
  11. return this.domainName;
  12. }
  13. }
  14. var website1=new WebSite("www.806501.com","806501","kavkill@806501.com",3,3000);
  15. console.log(website1);
  16. website1.setDoaminName("nodejs.806501.com");
  17. console.log(website1.getDomainName());
返回:
WebSite {
  domainName: ‘www.806501.com’,
  name: ‘806501’,
  email: ‘kavkill@806501.com’,
  age: 3,
  click: 3000,
  setDoaminName: [Function],
  getDomainName: [Function] }
nodejs.806501.com
5、使用prototype创建构造函数
  1. function JSWebSite(domainName,name,email,age,click){
  2. this.domainName=domainName;
  3. this.name=name;
  4. this.email=email;
  5. this.age=age;
  6. this.click=click;
  7. }
  8. JSWebSite.prototype.setDomainName=function(domainName){
  9. this.domainName=domainName;
  10. }
  11. JSWebSite.prototype.getDomainName=function(){
  12. return this.domainName;
  13. }
  14. var website=new JSWebSite("www.806501.com","kavkill","kavkill@806501.com",3,3000);
  15. console.log(website);
  16. website.setDomainName("kavkill.806501.com");
  17. console.log(website.getDomainName());
  18. console.log(website);
返回:
JSWebSite {
  domainName: ‘www.806501.com’,
  name: ‘kavkill’,
  email: ‘kavkill@806501.com’,
  age: 3,
  click: 3000 }
kavkill.806501.com
JSWebSite {
  domainName: ‘kavkill.806501.com’,
  name: ‘kavkill’,
  email: ‘kavkill@806501.com’,
  age: 3,
  click: 3000 }

附件列表

     

    留下评论

    电子邮件地址不会被公开。 必填项已用*标注