zoukankan      html  css  js  c++  java
  • javascript面向对象(学习和理解)

    js中创建变量基本如下:

    var name = 'saodiseng';
    var email = 'wuyucoder@126.com';
    var website = 'http://www.cnblogs.com/saodiseng/';

    如果把上面的东西换作变量来写呢?大胆改造一下:

    var saodiseng = {
            name:'saodiseng',
            email:'wuyucoder@126.com',
           website:'http://www.cnblogs.com/saodiseng/'
    };

    下面呢,我可以这样来访问它了:

    // 这个貌似是成员的方式
    saodiseng.name; saodiseng.email; saodiseng.website;

    // 这个貌似是hash map 的方式(说实话,hash map是什么,我还真的不知道)
    saodiseng["name"];
    saodiseng["email"];
    saodiseng["website"];

     如果写一个函数,我们通常是这样写的“

    var dosth = function(){
       alert("这是一个函数");
    };

    所以呢,下面我们可以这样来写了:

    var dazhaohu = function(){
      var hello = "Hello, 我是" + this.name
            + ",我的电子邮件是:" + this.email
            + ",我的个人博客地址是:" + this.website;

      alert(hello);
    }

    saodiseng.Hello =
    dazhaohu;
    saodiseng.Hello();
    
    

    如果更加规范的,看着更有逼格的写法:

    var Person = function(name. email, website){
      this.name = name;
      this.email = email;
      this.website = website;

      this.
    dazhaohu = function(){
        var hello = "Hello, 我是" + this.name
              + ",我的电子邮件是:" + this.email
              + ",我的个人博客地址是:" + this.website;
        alert(hello);
      };
    };

    var saodiseng = new Person('saodide',"wuyucoder@126.com","
    http://www.cnblogs.com/saodiseng/");
    saodiseng.dazhaohu();
  • 相关阅读:
    js+canvas画随机4位验证码
    linux 下 查看 nginx 日志中访问前10 的 ip
    mysql greatest函数
    php 如何获取 post 传递的raw 数据
    php 监控文件变化 并上传到服务器
    php 如何统计本周 本月
    Yii2.0 GridView 的强大功能
    git 导出新修改的文件
    ubuntu16.04 下安装phpMyAdmin
    如何在ubuntu16.04 上搭建 phpstorm + xdebug 调试
  • 原文地址:https://www.cnblogs.com/saodiseng/p/4700638.html
Copyright © 2011-2022 走看看