Front-end siege lion study NOTE 2: implements a class called man, which contains three methods: ATTR, words, and say.

Source: Internet
Author: User
Interview Questions

This is an interview with Sohu Javascript. The requirements are as follows:

Implement a class called man, which contains three methods: ATTR, words, and say.

 VaR  Man;  //  ++ Answer area ++  //  ++  Try  {  VaR Me = man ({fullname: "Xiaohong" });  VaR She = New MAN ({fullname: "Xiaohong"}); Console. Group (); console.info ( "My name is:" + me. ATTR ("fullname") + "\ n my gender is:" + me. ATTR ("gender" ); Console. groupend ();  /*  ------ [Execution result] ------ my name is: Xiaohong. My gender is: <user has not entered> ------------------  */  Me. ATTR ( "Fullname", "James" ); Me. ATTR ( "Gender", "male" ); Me. fullname = "Waste diesel" ; Me. Gender = "Renshile" ; She. ATTR ( "Gender", "female"); Console. Group (); console.info ( "My name is:" + me. ATTR ("fullname") + "\ n my gender is:" + me. ATTR ("gender" ); Console. groupend ();  /*  ------ [Execution result] ------ my name is: James. My gender is: Male ------------------  */  Console. Group (); console.info ( "My name is:" + She. ATTR ("fullname") + "\ n my gender is:" + She. ATTR ("gender" ); Console. groupend ();  /*  ------ [Execution result] ------ my name is: Xiaohong. My gender is: Female ------------------ */  Me. ATTR ({ "Words-limit": 3 , "Words-emote": "smile" }); Me. Words ( "I like watching videos. " ); Me. Words ( "Our office is so beautiful. " ); Me. Words ( "There are so many beautiful women in the video! " ); Me. Words ( "I usually watch Youku! " ); Console. Group (); console. Log (me. Say ());  /*  ------ [Execution result] ------ James smiled: "I like watching videos. Our office is so beautiful. There are so many beautiful women in the video! "------------------ */  Me. ATTR ({ "Words-limit": 2 , "Words-emote": "Shout" }); Console. Log (me. Say (); console. groupend ();  /*  ------ [Execution result] ------ James shouted: "I like watching videos. Our office is so beautiful. "------------------  */  }  Catch  (E) {console. Error ( "Execution error, error message:" + E );} 
Analysis Process

The analysis is as follows:

From the perspective of instantiating an object, you can use new or not. This is a scope Security constructor. The principle is to check whether this points to the current method, if not, a new object is forced out. The general framework is as follows:

 
Man =Function(OBJ ){If(This InstanceofArguments. callee ){For(VaREInOBJ ){This[E] =OBJ [e] ;}}Else{Return NewMAN (OBJ );}};

Through observation, we can find that the ATTR method can get or set the attribute value, and the parameter can be a string, a string plus a value, and an object. The ATTR method is defined as follows:

Man. Prototype. ATTR = Function  (ATTR, Val ){  If  (VAL ){  This [ATTR] = Val ;}  Else  {  If ( Typeof ATTR = "string" ){  Return   This . Hasownproperty (ATTR )?This [ATTR]: "<user not entered>" ;}  Else   If ( Typeof ATTR = "object" ){  For ( VaR E In  ATTR ){  This [E] = ATTR [e] ;}}  Else  {}}} 

By observing words, we can find that some strings are passed in for calling in the say method, so here we directly add an array of words_limit_arr to man to store these strings, therefore, the words method is implemented as follows:

 
Man. Prototype. Words =Function(STR ){This. Words_limit_arr.push (STR );}

Finally, we can see through observation that the say method is to add the value of the fullname attribute to the value of limit-emote, plus the strings connected by the previous words-limit words_limit_arr items, so the definition is as follows:

 
Man. Prototype. Say =Function(){Return This["Fullname"] +This["Words-emote"] + ":" + "\" "+This. Words_limit_arr.slice (0,This["Words-limit"]). Join ("") + "\"";}

The whole combination is:

VaR Man = Function  (OBJ ){  If ( This   Instanceof  Arguments. callee ){  For ( VaR E In  OBJ ){  This [E] = OBJ [E];}  This . Words_limit_arr =[];}  Else  {  Return   New  MAN (OBJ) ;}}; man. Prototype. ATTR = Function  (ATTR, Val ){  If  (VAL ){  This [ATTR] = Val ;}  Else  {  If (Typeof ATTR = "string" ){  Return   This . Hasownproperty (ATTR )? This [ATTR]: "<user not entered>" ;}  Else   If ( Typeof ATTR = "object" ){  For ( VaR E In ATTR ){  This [E] = ATTR [e] ;}}  Else  {}}} Man. Prototype. Words = Function  (STR ){  This  . Words_limit_arr.push (STR);} Man. Prototype. Say = Function  (){  Return   This ["Fullname"] + This ["Words-emote"] + ":" + "\" "+ This . Words_limit_arr.slice (0, This ["Words-limit"]). Join ("") + "\"" ;} 

However, there is a problem that cannot be solved, that is, the assignment of me. ATTR ("fullname", "XXX") method cannot be changed when the assignment of me. fullname = "XXX") method is used. The running result is as follows:

How can this problem be solved?CodeThe idea of that friend is to let me. ATTR ("fullname", "XXX") assign a value to fullname and me. fullname is not the same attribute.

Before thinking about other solutions, I had to modify my Code as follows:

 VaR Man = Function  (OBJ ){  If ( This  Instanceof  Arguments. callee ){  This . Infos = {  };  For ( VaR E In  OBJ ){  This . Infos [e] = OBJ [E];}  This . Words_limit_arr = [];}  Else {  Return   New  MAN (OBJ) ;}}; man. Prototype. ATTR = Function  (ATTR, Val ){  If  (VAL ){  This . Infos [ATTR] = Val ;}  Else  {  If ( Typeof ATTR = "string"){  Return   This . Infos [ATTR]? This . Infos [ATTR]: "<user not input>" ;}  Else   If ( Typeof ATTR = "object" ){  For ( VaR E In  ATTR ){  This . Infos [e] =ATTR [e] ;}}  Else  {}}} Man. Prototype. Words = Function  (STR ){  This  . Words_limit_arr.push (STR);} Man. Prototype. Say = Function  (){  Return   This . Infos ["fullname"] + This . Infos ["words-emote"] + ":" + "\" "+ This . Words_limit_arr.slice (0,This . Infos ["words-limit"]). Join ("") + "\"" ;} 

After modification, the running effect is as follows:

Summary

This topic mainly examines knowledge points such as scope Security constructor, instanceof/typeof, slice usage, array traversal, and object member traversal.

For the "use me. the value assignment in fullname = "XXX" mode cannot change me. assignment in ATTR ("fullname", "XXX") mode ". If you have any new solutions, please give us some advice. You are also welcome to discuss my solution.

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.