前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >js类公有属性和私有属性

js类公有属性和私有属性

作者头像
Java架构师必看
发布2021-08-19 10:48:59
5.4K0
发布2021-08-19 10:48:59
举报
文章被收录于专栏:Java架构师必看
代码语言:javascript
复制
function Test(){
	var name = 'test'//私有
	this.age = 12//公有
	this.getName = function(){
		return name
	}
	this.getAge = function(){
		return this.age
	}
}

var Test = new Test()
print(Test.name)//undefined
print(Test.age)//12
print(Test.getAge())//12
print(Test.getName())//'test'

模块模式

代码语言:javascript
复制
var singleton = function(){
	//私有变量和函数
	var privateVariable = 10
	function privateFunction(){
		return false
	}

	//创建对象
	var object = new CustomType()

	object.publicProperty = true;
	object.publicMethod = function(){
		privateVariable++
		return privateFunction()
	}

	return object;

    //特权/公有方法和属性
	// return {
	// 	publicProperty:true,
	// 	publicMethod:function(){
	// 		privateVariable++
	// 		return privateFunction()
	// 	}
	// }
}()
代码语言:javascript
复制
//单例接口
var application = function(){
	//私有
	var components = new Array()
	components.push(new BaseComponent())//BaseComponent()作为初始化操作

	var app = new BaseComponent()

	app.getComponentCount = function(){
		return components.length
	}

	app.registerComponent = function(component){
		if(typeof component == "object"){
			components.push(component)
		}
	}

	return app

	// return {
	// 	getComponentCount:function(){//返回已注册的组件数
	// 		return components.length
	// 	},
	// 	registerComponent:function(component){//注册新组件
	// 		if(typeof component == "object"){
	// 			components.push(component)
	// 		}
	// 	}
	// }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档