VBScript constructor and default keyword

Source: Internet
Author: User

VBScript is really a clumsy statement. end is the end, but it must be noted that it is the end, which is quite different from Ruby that uses the end!

 
Class user private [_ name] public property let name (NEO) [_ name] = NEO end property public property get name () 'no return statement, use "attribute name + equal sign + value" to implement name = "my name is" & [_ name] End propertyend classdim a set a = new usera. name = "situ zhengmei" msgbox (. name)

View the same JS implementation

 
Function user (name) {VaR _ name = Name; this. _ definesetter _ ("name", function (VAL) {_ name = val;}) This. _ definegetter _ ("name", function () {return "my name is" + _ name ;})} var user = new user ("situ zhengmei "); alert (user. name) // or function field (VAL) {This. value = val;} field. prototype = {get value () {return this. _ value + "!! ";}, Set value (VAL) {This. _ value = Val ;}}; var field = new field (" Nick "); alert (field. value)

VBScript does not have the constructor concept, but it has a strange default statement. Only one class is allowed. After the class is instantiated, it can be directly called like a function.

 
Class myclass public default function sayhello (name) 'Magic default statement sayhello = "hello," & name end function end class set O = new myclass msgbox O ("demon ") 'The instance is called like a function, it should be an object

All VBScript classes automatically support two events: class_initialize and class_terminate for initialization and destruction.

 
Class testclass 'setup initialize event. private sub class_initialize msgbox ("initialization") end sub 'setup terminate event. private sub class_terminate msgbox ("destroyed") end sub end class 'create an instance of testclass. set x = new testclass 'Destroy the instance. set x = nothing

With the default keyword, we can simulate the implementation of the constructor function:

 class rectangle private height, width' constructors public default function construtor (H, W) height = h: width = W set construtor = me end function 'method public property get area Area = height * width end propertyend class' does it look like a constructor? Set R = (New rectangle) (6, 8) msgbox R. area 

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.