标准用法:

function Sprite(){
//函数内容部设置属性
this.name='shimily';
}
//原型上设置方法
Sprite.prototype.show=function(){
console.log(this.name);
}
//【尽量不要在原型上面添加公共属性】
//公共的属性或常量可以在原型上面设置
Sprite.prototype.PI=3.1415926;
var s = new Sprite();
s.show();

改进用法:

改进用法一:*************************

function Sprite2(name,age){
//函数内容部设置属性
this.name=name;
this.age=age;
}
//原型上设置方法
Sprite2.prototype.show=function(){
console.log(this.name);
} var s2 = new Sprite2('shimily1314',20);
s2.show();

改进方法二:*******************

function Sprite3(options){
//函数内容部设置属性
this.name=options.name;
this.age=options.age;
}
//原型上设置方法
Sprite3.prototype.show=function(){
console.log(this.name);
}
var s3 = new Sprite3({
name:'shimilygood',
age:20
});
s3.show();

最终常用方法:***************

function Sprite4(options){
//函数内容部设置属性
this._init(options);
} Sprite4.prototype._init=function(options){
this.name=options.name;
this.age=options.age;
this.color=options.color;
}
//原型上设置方法
Sprite4.prototype.show=function(){
console.log(this.name);
} var s4 = new Sprite4({
name:'shimilygood123',
age:20
});
s4.show();

【最好、最常用】

最终【优化版】常用方法:***************

function Sprite5(options){
//函数内容部设置属性
this._init(options);
} Sprite5.prototype={
_init:function(options){ //只允许内部调用的方法【仅内部调用】
this.name=options.name;
this.age=options.age || 20;
this.color=options.color || 'red';
console.log(this.name);
},
show:function(ele){ //[可以不加参数]外部可以调用的方法不使用下划线
console.log(this.name + ele);
} };
var s5 = new Sprite5({
name:'shimilygoodabc',
age:20
});
s5.show('yang');

王伟峰,图片轮播开发案例格式*******挺好用的

function Slider(container, opts){                        //属性设置
this.$outer = $(container);
this.$inner = this.$outer.children();
this.$prev = $(opts.prev);
this.$next = $(opts.next);
this.$els = this.$inner.children();
this.total = this.$els.length;
this.w = this.$els.outerWidth(true);
this.timer = null;
this.isSliding = false;
this.autoplay = opts.autoplay || false;
this.init(); //对外接口
}
var proto = Slider.prototype; //原型中封装方法
proto.init = function(){
var self = this;
var $last = this.$els.eq(this.total-1);
if(this.total<6){
$last = this.$els.clone().appendTo(this.$inner).eq(this.total-1);
this.total *= 2;
}
$last.prependTo(this.$inner);
this.$inner.css('marginLeft', -this.w);
console.log(this.$next)
this.$prev.on('click', function(){
self.prev();
})
this.$next.on('click', function(){
self.next();
})
this.$outer.on('mouseenter', function(){
clearTimeout(self.timer);
})
this.$outer.on('mouseleave', function(){
self.auto();
})
this.auto();
}
proto.prev = function(){
if(this.isSliding) return;
this.isSliding = true;
var self = this;
this.$inner.animate({
marginLeft: 0
}, 500, function(){
self.$inner.children().eq(self.total-1).prependTo(self.$inner);
self.$inner.css('marginLeft', -self.w);
self.isSliding = false;
})
}
proto.next = function(){
if(this.isSliding) return;
this.isSliding = true;
var self = this;
this.$inner.animate({
marginLeft: -this.w*2
}, 500, function(){
self.$inner.children().eq(0).appendTo(self.$inner);
self.$inner.css('marginLeft', -self.w);
self.isSliding = false;
})
}
proto.auto = function(){
if(!this.autoplay) return;
var self = this;
function delay(){
self.timer = setTimeout(function(){
self.next();
delay();
}, 5000)
}
delay();
} //实例化
new Slider('.slideOuter',{
prev: '.prev',
next: '.next',
autoplay: true
});

js★★★【面向对象的使用方法】*****************★★★★ 相当重要的更多相关文章

  1. js面向对象 多种创建对象方法小结

    转自js面向对象 多种创建对象方法小结 1.对象字面量 var clock={ hour:12, minute:10, second:10, showTime:function(){ alert(th ...

  2. js面向对象的使用方法

    标准用法: function Sprite(){ //函数内容部设置属性 this.name='shimily'; } //原型上设置方法 Sprite.prototype.show=function ...

  3. js面向对象的封装方法,【案例】

    封装方法: /** * @矩形canvas库 * @authors Shimily (275766400@qq.com) * @date 2016-12-28 10:30:51 * @version ...

  4. JS面向对象(3) -- Object类,静态属性,闭包,私有属性, call和apply的使用,继承的三种实现方法

    相关链接: JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式 JS面向对象(2) -- this的使用,对 ...

  5. js面向对象+一般方法的选项卡

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. js面向对象之公有、私有、静态属性和方法详解

    现下,javascript大行其道,对于网站开发人员来说,javascript是必需掌据的一门语言,但随着jquery等框架的流行和使用,许多人对于原生javascript缺乏深入的理解,习惯了函数式 ...

  7. js面向对象自定义MyString()的构造器函数,实现内建String()属性和方法:

    js面向对象自定义MyString()的构造器函数,实现内建String()属性和方法: var s = new MyString('hello'); s.length; s[0]; // " ...

  8. JS面向对象编程,对象,属性,方法。

    document.write('<script type="text/javascript" src="http://api.map.baidu.com/api?v ...

  9. js面向对象初步探究(上) js面向对象的5种写方法

    非常长一段时间看网上大神的JS代码特别吃力.那种面向对象的写法方式让人看得云里来雾里去.于是就研究了一下JS面向对象.因为是初学,就将自己在网上找到的资料整理一下,作为记忆. js面向对象的5种写方法 ...

  10. JavaScript---正则使用,日期Date的使用,Math的使用,JS面向对象(工厂模式,元模型创建对象,Object添加方法)

    JavaScript---正则使用,日期Date的使用,Math的使用,JS面向对象(工厂模式,元模型创建对象,Object添加方法) 一丶正则的用法 创建正则对象: 方式一: var reg=new ...

随机推荐

  1. radio(单选框)反复选中与取消选中

    做个记录,以便需要拿取 <script type="text/javascript"> $(function(){ 第一种 $('input:radio').click ...

  2. Poj2018 Best Cow Fences

    传送门 题目大意就是给定一个长度为 n 的正整数序列 A ,求一个平均数最大的,长度不小于 L 的子序列. 思路: 二分答案. Code: #include<iostream> #incl ...

  3. springboot(一)

    @Configuration:声明当前类是一个配置类==等同于 spring的xml文件,如果使用了改注解意味着该类里可能有0个或者多个@bean注解,此处没有使用包扫描,是因为所有的@bean 都在 ...

  4. leecode第二百九十二题(Nim游戏)

    class Solution { public: bool canWinNim(int n) { )==)//用与的时候,要注意优先级问题 //用n%4==0的时候,其耗时比用&短,但是空间消 ...

  5. PyQt5——高级控件

    PyQt5高级控件使用方法详见:https://blog.csdn.net/jia666666/article/list/4?t=1& PyQt5高级控件汇总: 1.QTableView 2. ...

  6. Phonics 自然拼读法 g, o, u, l, f, b Teacher:Lamb

    课上内容(Lesson) Part I     Warm-up & Video  Food Fashion Fun  19 sets http://www.unsv.com/material/ ...

  7. es索引管理工具-curator

    elasticsearch-curator  是官方收购的开源社区周边产品,用来管理es的索引和快照. 官方文档:https://www.elastic.co/guide/en/elasticsear ...

  8. 配置Spring MVC - 2019

    未完 软件环境:Eclipse-EE 1. 创建Maven Project 2. pom.xml - [更新日期19/03/31] <dependencies> <dependenc ...

  9. 解决[Errno 10048]报错

    最近一直很纳闷,写的python程序在自己电脑上运行得非常顺利,放到远程测试机上经常报“[Errno 10048]通常每个套接字地址(协议/网络地址/端口)“这样的错误,百度到这种是端口号冲突引起的, ...

  10. maven将依赖的jar包复制到指定位置

    <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> ...