zoukankan      html  css  js  c++  java
  • Ember学习(8):REOPENING CLASSES AND INSTANCES

    英文原址:http://emberjs.com/guides/object-model/reopening-classes-and-instances/


    你不须要一次就完毕类所有内容的定义,通过reopen方法,你能够又一次打开一个类定义而且加入新的属性。

    1
    2
    3
    4
    5

    Person.reopen({
     
    isPerson: true
    });

    Person.create().get(
    'isPerson') // true


    当使用reopen时,你也能够重写已有的方法,通过调用this._super调用之前的版本号

    1
    2
    3
    4
    5
    6

    Person.reopen({
     
    // override `say` to add an ! at the end
     
    say: function(thing) {
       
    this._super(thing + "!");
      }
    });

    reopen方法是用于给类加入新的实例方法和属性的,这些新加入的属性和方法会被全部该类的实例所共享,它不是只只给该类的某个特定的实例加入方法和属性(好比普通的Javascript下不使用prototype的情况)

     

    假设你须要给类本身加入新的类属性或者类方法,你能够使用reopenClass

    1
    2
    3
    4
    5
    6
    7

    Person.reopenClass({
     
    createMan: function() {
       
    return Person.create({isMan: true})
      }
    });

    Person.createMan().get(
    'isMan') // true


  • 相关阅读:
    bootstrap
    Bootstrap面试题
    h5+css3 考试题
    less安装与使用
    css3(四)响应式 Web 设计 - 媒体查询
    css3(三)弹性布局
    css3(三)多列布局
    JavaScriptAPI初识
    Bootstrap面试题
    BS初识
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/3763311.html
Copyright © 2011-2022 走看看