得于吾师傅的js知识 js类,单写模板,和私有保护的方法

  js的类的写法:

1,写法一:function内部包含this.function()如代码:

var origin_class = function(name)
{
  var lover = '';
  this.getLover = function(hername)
 {
  return this.lover = hername;
 }
  this.show = function(hername)
 {
  return this.getLover(hername)+'love'+name;
 }
}
var talk = new origin_class('modle');
talk.getLover('zhongyaji');
talk.show('zhongyaji');

2,用对象结构法(不能够实例化,但是也算是一个类吧,因为包含了许多方法):

var modle = 
{
 sayHello:function()
 {
  console.log('hello');
 }
};
modle.sayHello();

3,原形法(实际上仅仅用于继承,十分类似于java的继承之类):

var hello = function(){
this.you = function(){
console.log('you');
}
}
//这里是继承,像java一样,在实例化前继承才可,还有一种写法
//这种啊,由于继承,不可再this.me了,都继承了,怎么可能还是自己呢?是吧?
hello.protorype =
{
me:function()
{
console.log('me');
}
} hello.prototype.me
=function()
{
console.log('me');
}
var show = new hello(); show.me();

4,关于对象中的保护不暴露

var app = 
{
    //类似于构造函数,执行了init整个作用于才生效,否则仅仅是定义.
    init:function()
    {

    },
    showLove:function()
    {
        console.log('show');
    },
    hello:function()
    {
        console.log('fuck!')
    }
}
//如果其中方法有的是私有的,不想暴露,那么如此
var output = 
{
    //如此便可以不暴露app当中的一些方法
    hello:app.showLove();
}
app.init();

5,还有一个方法

$('#selector').load('index.php #container');

意思有点类似于ajax的get方法,得到index.php的#container中间的内容

原文地址:https://www.cnblogs.com/modle-sherlock/p/5246067.html