标准用法:

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

改进用法:

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

  1. function Sprite2(name,age){
  2. //函数内容部设置属性
  3. this.name=name;
  4. this.age=age;
  5. }
  6. //原型上设置方法
    Sprite2.prototype.show=function(){
  7. console.log(this.name);
  8. }
  9.  
  10. var s2 = new Sprite2('shimily1314',20);
  11. s2.show();

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

  1. function Sprite3(options){
  2. //函数内容部设置属性
  3. this.name=options.name;
  4. this.age=options.age;
  5. }
  6. //原型上设置方法
  7. Sprite3.prototype.show=function(){
  8. console.log(this.name);
  9. }
  10. var s3 = new Sprite3({
  11. name:'shimilygood',
  12. age:20
    });
  13. s3.show();

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

  1. function Sprite4(options){
  2. //函数内容部设置属性
  3. this._init(options);
  4. }
  5.  
  6. Sprite4.prototype._init=function(options){
  7. this.name=options.name;
  8. this.age=options.age;
  9. this.color=options.color;
  10. }
  11. //原型上设置方法
  12. Sprite4.prototype.show=function(){
  13. console.log(this.name);
  14. }
  15.  
  16. var s4 = new Sprite4({
  17. name:'shimilygood123',
  18. age:20
  19. });
  20. s4.show();

【最好、最常用】

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

  1. function Sprite5(options){
  2. //函数内容部设置属性
  3. this._init(options);
  4. }
  5.  
  6. Sprite5.prototype={
  7. _init:function(options){ //只允许内部调用的方法【仅内部调用】
  8. this.name=options.name;
  9. this.age=options.age || 20;
  10. this.color=options.color || 'red';
  11. console.log(this.name);
  12. },
  13. show:function(ele){ //[可以不加参数]外部可以调用的方法不使用下划线
  14. console.log(this.name + ele);
  15. }
  16.  
  17. };
  18. var s5 = new Sprite5({
  19. name:'shimilygoodabc',
  20. age:20
  21. });
  22. s5.show('yang');

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

  1. function Slider(container, opts){ //属性设置
  2. this.$outer = $(container);
  3. this.$inner = this.$outer.children();
  4. this.$prev = $(opts.prev);
  5. this.$next = $(opts.next);
  6. this.$els = this.$inner.children();
  7. this.total = this.$els.length;
  8. this.w = this.$els.outerWidth(true);
  9. this.timer = null;
  10. this.isSliding = false;
  11. this.autoplay = opts.autoplay || false;
  12. this.init(); //对外接口
  13. }
  14. var proto = Slider.prototype; //原型中封装方法
  15. proto.init = function(){
  16. var self = this;
  17. var $last = this.$els.eq(this.total-1);
  18. if(this.total<6){
  19. $last = this.$els.clone().appendTo(this.$inner).eq(this.total-1);
  20. this.total *= 2;
  21. }
  22. $last.prependTo(this.$inner);
  23. this.$inner.css('marginLeft', -this.w);
  24. console.log(this.$next)
  25. this.$prev.on('click', function(){
  26. self.prev();
  27. })
  28. this.$next.on('click', function(){
  29. self.next();
  30. })
  31. this.$outer.on('mouseenter', function(){
  32. clearTimeout(self.timer);
  33. })
  34. this.$outer.on('mouseleave', function(){
  35. self.auto();
  36. })
  37. this.auto();
  38. }
  39. proto.prev = function(){
  40. if(this.isSliding) return;
  41. this.isSliding = true;
  42. var self = this;
  43. this.$inner.animate({
  44. marginLeft: 0
  45. }, 500, function(){
  46. self.$inner.children().eq(self.total-1).prependTo(self.$inner);
  47. self.$inner.css('marginLeft', -self.w);
  48. self.isSliding = false;
  49. })
  50. }
  51. proto.next = function(){
  52. if(this.isSliding) return;
  53. this.isSliding = true;
  54. var self = this;
  55. this.$inner.animate({
  56. marginLeft: -this.w*2
  57. }, 500, function(){
  58. self.$inner.children().eq(0).appendTo(self.$inner);
  59. self.$inner.css('marginLeft', -self.w);
  60. self.isSliding = false;
  61. })
  62. }
  63. proto.auto = function(){
  64. if(!this.autoplay) return;
  65. var self = this;
  66. function delay(){
  67. self.timer = setTimeout(function(){
  68. self.next();
  69. delay();
  70. }, 5000)
  71. }
  72. delay();
  73. }
  74.  
  75. //实例化
  76. new Slider('.slideOuter',{
  77. prev: '.prev',
  78. next: '.next',
  79. autoplay: true
  80. });

js面向对象的使用方法的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. table 标签

    <table border="1" width="100%"> <thead align="left"> <t ...

  2. Mac OSX上的软件包管理工具,brew 即 Homebrew

    brew 即 Homebrew,是Mac OSX上的软件包管理工具,能在Mac中方便的安装软件或者卸载软件, 只需要一个命令, 非常方便. brew类似ubuntu系统下的apt-get的功能. 安装 ...

  3. angular的$scope

    angularJS是一个MVVM的前端js框架. $scope的作用是angular向视图(html)传递数据的通道,它不负责处理和操作数据.也就是说要想向视图传递数据的话,必须定义$scope变量. ...

  4. 从零开始学习Android(一)Android环境的搭建

    好久没有开始写博客了,最近开始学习Android,所以想把学习的笔记都一一记录下来.一来是方便自己以后资料的查询,其次也是给Android新手朋友进行学习使用,再次也希 望得到高手的指点.废话少说,我 ...

  5. [转]让窗体不显示在Alt+Tab中

    public class MyForm : Form { protected override CreateParams CreateParams { get { const int WS_EX_AP ...

  6. Go语言语法汇总(转)

    Go语言语法汇总 分类: 技术2013-09-16 14:21 3007人阅读 评论(0) 收藏 举报 go语言golang并发语法   目录(?)[+]   最近看了看GoLang,把Go语言的语法 ...

  7. java回调初步学习

    转帖请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17483273),请尊重他人的辛勤劳动成果,谢谢以前不理解什 ...

  8. Ubuntu install g++

    We can use two ways to  install g++ on Ubuntu. 1.  a. sudo apt-get install make gcc g++.      b. sud ...

  9. HttpClient 4.5.x 工具类设计与实现

    最近,业务需要在java服务端发起http请求,需要实现"GET","POST","PUT"等基本方法.于是想以 "HttpCli ...

  10. jQuery index()

    index() index() 方法返回指定元素相对于其他指定元素的 index 位置. 语法 $(selector1).index(selector2) selector2:可选,指定元素:为空时默 ...