zoukankan      html  css  js  c++  java
  • javascript的类、委托、事件

    javascript中的类:


    function Person(name, age) {
                
    this._name = name;
                
    this._age = age;
                
    //对应Name的Get,Set方法,这个和Java的属性写法很像。
                this.getName = function() {
                    
    return this._name
                };
                
    this.setName = function(name) {
                    
    this._name = name;
                }
                
    //对应Age的Get,Set方法
                this.getAge = function() {
                    
    return this._age;
                }
                
    this.setAge = function(age) {
                    
    this._age = age;
                }
                
    //显示Person的信息方法
                this.show = function() {
                    alert(
    "Name:" + this.getName() + "; Age:" + this.getAge());
                }
            }

            
    //空构造方法
            var p1 = new Person();
            p1.setName(
    "Southsea");
            p1.setAge(
    23);
            p1.show();

            
    //带参的构造方法
            var p2 = new Person("Southsea"23);
            p2.show();
            
            
    //注:Javascript中没有真正的方法重载

    看起来很简单吧。

    下面我们把Pererson类的show方法加一个参数,让它具有委托的功能。


            function Person(name, age) {
                
    this._name = name;
                
    this._age = age;
                
    //对应Name的Get,Set方法,这个和Java的属性写法很像。
                this.getName = function() {
                    
    return this._name
                };
                
    this.setName = function(name) {
                    
    this._name = name;
                }
                
    //对应Age的Get,Set方法
                this.getAge = function() {
                    
    return this._age;
                }
                
    this.setAge = function(age) {
                    
    this._age = age;
                }
                
    //显示Person的信息方法
                this.show = function(delegate) {
                    
    if (delegate) {
                        
    delegate(this);
                    }
                }
    //只有这段与上面的不同。
            }

            
    //订阅Person类的show
            function showPerson(p) {
                alert(
    "Name:" + p.getName() + "; Age:" + p.getAge());
            }

            var p 
    = new Person("Southsea"23);
            p.show(showPerson); 
    //别写成p.show(showPerson());哦

    javascript中的事件


            function Person(name, age) {
                
    this._name = name;
                
    this._age = age;
                
    //对应Name的Get,Set方法,这个和Java的属性写法很像。
                this.getName = function() {
                    
    return this._name
                };
                
    this.setName = function(name) {
                    
    this._name = name;
                }
                
    //对应Age的Get,Set方法
                this.getAge = function() {
                    
    return this._age;
                }
                
    this.setAge = function(age) {
                    
    this._age = age;
                }
                
    this.onShow = null;//加了onshow事件
                
    //显示Person的信息方法
                this.show = function() {
                    
    if (this.onShow) {
                        
    this.onShow(this);
                    }
                }
            }

            
    //订阅Person类的show
            function showPerson(p) {
                alert(
    "Name:" + p.getName() + "; Age:" + p.getAge());
            }

            var p 
    = new Person("Southsea"23);
            p.onShow 
    = showPerson; //千万别写成p.onShow = showPerson();
            p.show(); 

    委托和事件都看起来很简单吧。

    javascript的动态类,它的格式是与JSON一样的。


            var person = {
                
    "Name""Southsea",
                
    "Age"23"show": function() {
                    alert(
    "Name:" + person.Name + "; Age:" + person.Age);
                }
            };
            person.show();
  • 相关阅读:
    Android开发——弹性滑动的两种实现方式
    管理知识和解决信息爆炸问题的4种方法
    京东金融的业务版图
    京东金融的业务版图
    虚幻引擎4艺术大师
    Android开发——View滑动的三种实现方式
    Android开发之Path类使用详解,自绘各种各样的图形!
    C# Dictionary的遍历理解
    我想走全产业链发展路线
    Androd安全——混淆技术完全解析
  • 原文地址:https://www.cnblogs.com/BlogNetSpace/p/1501245.html
Copyright © 2011-2022 走看看