javascript中通过 prototype定义的属性 在方法中如何访问,向外部抛出方法。。
<script type="text/javascript"> // 定义一个 Fun 类, public class Fun {} function Fun() { // 这里定义一个方法,调用 Fun 类中的 name 属性,必须使用 this.name 代表该对象.name属性 this.say = function() { alert("你好,我是" + this.name); } } // 为 Fun 绑定属性和方法 Fun.prototype = { // 绑定 name 属性, var name = "zhong"; name: "zhong" }; window.onload = function() { // 必须使用 new Fun();实例化 Fun 类,alert 出他的 name 值 alert(new Fun().name); // 必须使用 new Fun();实例化 Fun 类,调用 say() 方法 new Fun().say(); } </script>
例如:
function Letter(bg,els,opts,n,money) { this.$Dialog = $(els); this.$Dialogbg = $(bg); this.$close = this.$Dialog.find(opts.close); this.$btn = this.$Dialog.find(opts.btn); this.n = n; this.money = money; this._init(); } Letter.prototype={ _init:function () { var self = this; this.show(); /*this.$Dialogbg.click(function () { self.hide(); });*/ this.$close.click(function () { self.hide(); }); /*this.$btn.click(function () { self.hide(); });*/ }, show:function () { this.$Dialogbg.fadeIn(300); this.$Dialog.fadeIn(300); this.$Dialog.find(".hb").text(this.money); this.$Dialog.find(".quan-"+this.n).fadeIn(300).siblings().not(".closes").hide(); }, hide:function () { this.$Dialogbg.fadeOut(300); this.$Dialog.fadeOut(300); $(".sec-qingshu .content,.sec-qingshu .qs-btn").addClass("open"); } };
在外部访问hide方法:
$(".go-use").click(function () {
new Letter(".Dialogbg",'.DialogBox',1,10).hide(); //必须使用 new Letter();实例化 Letter 类,调用 hide() 方法
});
给心灵一个纯净空间,让思想,情感,飞扬!