Ember.js Getting Started Guide--extensions (reopen)

Source: Internet
Author: User


This series of articles is all from (http://ibeginner.sinaapp.com/), please visit the original website. 1, extending general properties

reopen I do not know how to translate, if according to Reopen translation should be . but always feel bad, so replace it with " reopen () extend () method you need to redefine a subclass , and then add a new property, method to the child class.

< Span style= "LINE-HEIGHT:17.12PX; Font-family:calibri; " >        The previous article, Call create () reopen () . Span style= "LINE-HEIGHT:17.12PX; Font-family:simsun; " > The method can pass in new parameters. With

Parent = ember.object.extend ({    name:  ' Ubuntuvim ',    &NBSP;FUN1 ()  {        console.log ("The name is "  + this.name);     },    common ()  {        console.log ("Common method ...");     });    //    use the Extend () method to add a new property, Method Child1 = parent.extend ({    //   Add a property to the class parent     pwd:  ' 12345 ',    //   to the class parent to add a new method     fun2 ()  {        console.log ("The pwd  is  " + this.pwd);    },    //      Overriding the parent class's Common () method     common ()  {        // Console.log ("Override cOmmon method of parent ");         this._super ();     });    var c1 = child1.create (); Console.log ("name =   " + c1.get (' name ')  + ", pwd =  " + c1.get (' pwd ');   c1.fun1 (); c1.fun2 ();     c1.common (); Console.log ("-----------------------");       //   use the reopen () method to add a new property, method Parent.reopen ({    //    give class parent a new property     pwd:  ' 12345 ',    //   Add a new method to the class parent     fun2 ()  {         Console.log ("the pwd is "  + this.pwd);    },     //   Rewrite class itself common () method     common ()  {         console.log ("override comMon method by reopen method ... ");         //this._ Super ();     }); Var p = parent.create ();    console.log ("Name  =  " + p.get (' name ')  + ", pwd =  " + p.get (' pwd ')); &NBSP;&NBSP;P.FUN1 ();p. fun2 ();    p.common (); Console.log ("---------------------------");     //   use the Extend () method to add a new property, Method Child2 = parent.extend ({     //   give class parent a new property     pwd:  ' 12345 ',    //    Give class parent a new Method     fun2 ()  {         Console.log ("the pwd is "  + this.pwd);    },     //     overriding the parent class's Common () method     common ()  {         //consoLe.log ("Override common method of parent ...");         this._super ();     });    var c2 = child2.create (); Console.log ("name = "  + c2.get (' name ')  +  ", pwd = "  +  C2.get (' pwd '));   c2.fun1 (); c2.fun2 (); C2.common ();

The following differences can be seen from the execution results :

Same point:

can be used to extend a class.

different points:

      • Extend need to redefine a class and inherit the extended class;

      • Reopen is a new property and method on the extended class itself;

In the end,the reopen method will change the behavior of the original class, just as the demo instance is called after the reopen method common of the Child2 class. The behavior of the method has been changed, the coding process has been forgotten before the reopen method has been called may appear that they do not know how the problem!

if it is Extend Method leads to more and more classes, the inheritance tree will be more and more deep, the performance, debugging is also a major challenge, but Extend does not change the behavior of the inherited class.


2, extending static properties

use the Reopenclass () method to extend the properties, methods of the static type

Parent = Ember.Object.extend (); Use the Reopenclass () method to add a new static property, method Parent.reopenclass ({isperson:true,//name: ' 123 '//Cannot set the property of String type!!!) , reported uncaught Typeerror:cannot assign to read the property ' name '});  Parent.reopen ({isperson:false, Name: ' Ubuntuvim '}); Console.log (Parent.isperson); Console.log (Parent.name);    Output null Console.log (Parent.create (). Get (' Isperson ')); Console.log (Parent.create (). Get (' name ')); Output Ubuntuvim

But there is a doubt that Reopenclass () added string Type property when prompted not to allow!!! The official website tutorial does not explain this problem, it is estimated that the code has a problem. Have time to see what's going on ...


Ember.js Getting Started Guide--extensions (reopen)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.