<script> <br>//Define a javascript class<br>function JsClass(privateParam/* */,publicParam){//Constructor<br>var priMember = privateParam; //Private variable<br>this.pubMember = publicParam; //Public variable<br>//Define private method <br>function priMethod(){ <br>return "priMethod()"; <br>} <br>//Define privileged methods<br>//Privileged methods can access all members<br>this.privilegedMethod = function( ){ <br>var str = "This is a privileged method, I called
"; <br>str = " Private variable: " priMember "n"; <br>str = " Private method: " priMethod() "n"; <br>str = " Public variable: " this.pubMember "n" ; <br>str = "Public method:" this.pubMethod(); <br><br>return str; <br>} <br>} <br>//Add public method <br>//Cannot call private Variables and methods <br>JsClass.prototype.pubMethod = function(){ <br>return "pubMethod()"; <br>} <br><br>//Use an instance of JsClass <br>JsObject = new JsClass( "priMember","pubMember"); <br><br>//alert(JsObject.pubMember);//Pop up pubMember information<br>//alert(JsObject.priMember);//Pop up undefined information<br>/ /alert(JsObject.pubMethod());//Pop up pubMethod information<br>//alert(JsObject.priMethod());//Pop up the error "The object does not support this property or method"<br>alert(JsObject. privilegedMethod()); <br></script>
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn