经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JSJS库框架 » JavaScript » 查看文章
JavaScript初识(三)
来源:cnblogs  作者:七寸丶  时间:2018/9/28 16:51:48  对本文有异议

十三丶JS中的面向对象

  创建对象的几种常用方式:

    1.使用Object或对象字面量创建对象

    2.工厂模式创建对象

    3.构造函数模式创建对象

    4.原型模式创建对象

  下面我们详细看一下如何创建对象

  1.使用Object或对象字面量创建对象

  JS中最基本创建对象的方式:

  1. <script type="text/javascript">
  2. var student = new Object();
  3. student.name = "alex";
  4. student.age = "20"
  5. </script>

  字面量方式创建对象:

  1. var student = {
  2. name:"alex",
  3. age:18
  4. };

  2.工厂模式创建对象

  以上的方式看似简便,但是我们要是创建很多个同类的呢?我们是不是得把以上代码重复n次呢,是否可以像工厂车间那样,不断生产呢?那就让我们看看工厂车间那样,如何"产出"对象

  1. function createStudent(name,age){
  2. var obj = new Object();
  3. obj.name = name;
  4. obj.age = age;
  5. return obj;
  6. }
  7. var student1 = createStudent('easy',20);
  8. var student2 = createStudent('easy2',20)
  9. ...
  10. var studentn = createStudent('easyn',20)

  3.构造函数模式创建对象

    在上面创建Object这样的原生对象的时候,我们就使用过其构造函数:

  1. var obj = new Object();

    在创建原生数组Array类型对象时也使用过其构造函数:

  1. var arr = new Array(10); //构造一个初始长度为10的数组对象

  在进行自定义构造函数创建对象之前,我们先了解一下构造函数和普通函数有什么区别.

  1丶实际上并不存在创建构造函数的特殊语法,其与普通函数唯一的区别在于调用方法.对于任意函数,使用new操作符调用,那么它就是构造函数;不使用new操作符调用,那么它就是普通函数.

  2丶按照惯例,我们约定构造函数名以大写字母开头,普通函数以小写字母开头,这样有利于显性区分二者,例如上面的new Array(),new Object().

  3.使用new操作符调用构造函数时,会经历(1)创建一个新对象(2)将构造函数作用域赋给新对象(指this指向该新对象)(3)执行构造函数代码(4)返回新对象;4个阶段

  我们使用构造函数将工厂模式的函数重写,并添加一个方法属性

  1. function Student(name,age){
  2. this.name = name;
  3. this.age = age;
  4. this.alertName = function(){
  5. alert(this.name)
  6. };
  7. }
  8. function Fruit(name,color){
  9. this.name = name;
  10. this.color = color;
  11. this.alertName = function(){
  12. alert(this.name)
  13. };
  14. }

  4.原型的模式创建对象

    原型链甚至原型继承,是整个JS中最难的一部分,也是最不好理解的一部分.

  1. //原型模式创建对象
  2. function Student(){
  3. this.name = "easy";
  4. this.age = 20;
  5. }
  6. Student.prototype.alertName = function(){
  7. alert(this.name);
  8. };
  9. var stu1 = new Student();
  10. var stu2 = new Student();
  11. stu1.alertName(); //easy
  12. stu2.alertName(); //easy
  13. alert(stu1.alertName == stu2.alertName); //true 二者共享同一函数

十四丶定时器

  (1)一次性定时器

    可以做异步

  (2)循环周期定时器

    可以做动画

  JS跟python一样都有垃圾回收机制,但是定时器对象垃圾回收是回收不了的

  1.setTimeOut()一次性定时器,只在指定时间后执行一次

  1. <script type="text/javascript">
  2. <!--一次性定时器-->
  3. function hello(){
  4. alert("hello");
  5. }
  6. <!--使用方法名字执行方法-->
  7. var t1 = window.setTimeout('hello',1000);
  8. var t2 = window.setTimeout("hello()",3000);//使用字符串执行方法
  9. window.cleatTimeout(t1);//去掉定时器
  10. </script>

  2.setInterval()

  1. //循环周期定时器
  2. setInterval('refreshQuery()',8000);
  3. function refreshQuery(){
  4. console.log("每8秒调一次")
  5. }

  练习:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. #box{
  8. width: 200px;
  9. height: 200px;
  10. background-color: red;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <button id = "start">开启定时器</button>
  16. <button id = "clear">清除定时器</button>
  17. <div id="box"></div>
  18. <script type="text/javascript">
  19. var count = 0;
  20. var timer = null;
  21. document.getElementById("start").onclick = function(){
  22. var oDiv = document.getElementById("box");
  23. clearInterval(timer);
  24. timer = setInterval(function(){
  25. count += 10;
  26. oDiv.style.marginLeft = count + "px";
  27. oDiv.style.marginTop = count/2 +"px"
  28. },50)
  29. }
  30. </script>
  31. </body>
  32. </html>
View Code

十五丶BOM的介绍

    BOM; Browser Object Model,浏览器对象模型.

  window对象是BOM的顶层(核心)对象,所有对象都是通过它延伸出来的,也可以成为window对象的子对象,DOM是BOM的一部分.

  1丶弹出系统对话框

  比如说,alert(1)是window.alert(1)的简写,以为它是window的子方法.

  系统对话框有三种:

  1. alert(); //不同浏览器中的外观是不一样的
  2. confirm(); //兼容不好
  3. prompt(); //不推荐使用

  2.打开窗口丶关闭窗口

    (1)打开窗口:

  1. window.open(url,target)

  url:要打开的地址

  target:新窗口的位置.可以是:_blank丶_self丶_parent父框架

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. <!--行间的js中的open() window不能省略-->
  9. <button onclick="window.open('https://www.luffycity.com/')">路飞学城</button>
  10. <button>打开百度</button>
  11. <button onclick="window.close()">关闭</button>
  12. <button>关闭</button>
  13. </body>
  14. <script type="text/javascript">
  15. var oBtn = document.getElementsByTagName('button')[1];
  16. var closeBtn = document.getElementsByTagName('button')[3];
  17. oBtn.onclick = function(){
  18. open('https://www.baidu.com')
  19. //打开空白页面
  20. // open('about:blank',"_self")
  21. }
  22. closeBtn.onclick = function(){
  23. if(confirm("是否关闭?")){
  24. close();
  25. }
  26. }
  27. </script>
  28. </html>

  location对象

    window.location可以简写成location.location 相当于浏览器地址栏,可以将url解析成独立的片段.

  location对象的属性

    href:跳转

    hash 返回url中#后面的内容,包括#

    host 主机名,包括端口

    hostname 主机名

    pathname url中的路径部分

    protocol 协议一般是http丶https

    search 查询字符串

  location.href属性举例:

    点击盒子时,进行跳转。

  1. <body>
  2. <div>smyhvae</div>
  3. <script>
  4.  
  5. var div = document.getElementsByTagName("div")[0];
  6. div.onclick = function () {
  7. location.href = "http://www.baidu.com"; //点击div时,跳转到指定链接
  8. // window.open("http://www.baidu.com","_blank"); //方式二
  9. }
  10. </script>
  11. </body>

    5秒后自动跳转到百度。

  1. <script>
  2. setTimeout(function () {
  3. location.href = "http://www.baidu.com";
  4. }, 5000);
  5. </script>

  location.reload():重新加载

  1. setTimeout(function(){
  2. //3秒之后让网页整个刷新
  3. window.location.reload();
  4. },3000)

  navigator对象

  window.navigator 的一些属性可以获取客户端的一些信息。

    userAgent:系统丶浏览器

    platform;浏览器支持的系统,win/mac/linux

  1. console.log(navigator.userAgent);
  2. console.log(navigator.platform);

  history对象

  1、后退:

      •  history.back()

      •  history.go(-1):0是刷新

  2、前进:

      •  history.forward()

      •  history.go(1)

    用的不多。因为浏览器中已经自带了这些功能的按钮:

 十六丶client丶offset丶scroll系列

  先来了解一下自执行函数:

  1. (function(window) {
  2. var a = 5;
  3. // import
  4. window.$ = a;
  5. })(window);
1.js

 

  1. (function(window) {
  2. var a = 6;
  3. window.$1 = a;
  4. })(window);
2.js
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. </head>
  7. <body>
  8. <script src="1.js"></script>
  9. <script src="2.js"></script>
  10. <script>
  11. console.log(window.$);
  12. console.log(window.$1);
  13. </script>
  14. </body>
  15. </html>
自执行函数

  1.client系列

    clientTop  内容区域到边框顶部的距离,说白了,就是边框高度

    clietLeft    内容区域到边框左部的距离,说白了就是边框的宽度

    clientWidth   内容区域+左右padding  不包含border  可视宽度

    clientHeight  内容区域+ 上下padding  可视高度

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style type="text/css">
  7. .box{
  8. width: 200px;
  9. height: 200px;
  10. /*position: absolute;*/
  11. border: 10px solid red;
  12. /*margin: 10px 0px 0px 0px;*/
  13. padding: 80px;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div class="box">
  19. 哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈
  20. </div>
  21. </body>
  22. <script type="text/javascript">
  23. /*
  24. * clientTop 内容区域到边框顶部的距离 ,说白了,就是边框的高度
  25. * clientLeft 内容区域到边框左部的距离,说白了就是边框的宽度
  26. * clientWidth 内容区域+左右padding 不包含border 可视宽度
  27. * clientHeight 内容区域+ 上下padding 可视高度
  28. * */
  29. var oBox = document.getElementsByClassName('box')[0];
  30. console.log(oBox.clientTop);
  31. console.log(oBox.clientLeft);
  32. console.log(oBox.clientWidth);
  33. console.log(oBox.clientHeight);
  34. </script>
  35. </html>
View Code

  2.屏幕的可视区域

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. //屏幕 的可视区域
  10. window.onload = function(){
  11. //document.documentElement 获取的是html标签
  12. console.log(document.documentElement.clientWidth);
  13. console.log(document.documentElement.clientHeight);
  14. //窗口大小发生变化时,会调用此方法
  15. window.onresize = function(){
  16. console.log(document.documentElement.clientWidth);
  17. console.log(document.documentElement.clientHeight);
  18. }
  19. }
  20. </script>
  21. </body>
  22. </html>

  3.offset系列

    offsetWidth占位宽 内容+padding+border

    offsetHeight 占位高

    offsetTop  如果盒子没有设置定位到body的顶部的距离,如果盒子设置定位,那么是以父辈为基准的Top值

    offsetLeft:如果盒子没有设置定位到body的左部的距离,如果盒子设置定位,那么是以父辈为基准的left值

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style type="text/css">
  7. *{
  8. padding: 0;
  9. margin: 0;
  10. }
  11. </style>
  12. </head>
  13. <body style="height: 2000px">
  14. <div>
  15. <div class="wrap" style=" width: 300px;height: 300px;background-color: green;position: relative; top: 20px;">
  16. <div id="box" style="width: 200px;height: 200px;border: 5px solid red;position: absolute;top:60px;left: 30px;">
  17. </div>
  18. </div>
  19. </div>
  20. </body>
  21. <script type="text/javascript">
  22. window.onload = function(){
  23. var box = document.getElementById('box')
  24. /*
  25. offsetWidth占位宽 内容+padding+border
  26. offsetHeight占位高
  27. * offsetTop: 如果盒子没有设置定位 到body的顶部的距离,如果盒子设置定位,那么是以父辈为基准的top值
  28. * offsetLeft: 如果盒子没有设置定位 到body的左部的距离,如果盒子设置定位,那么是以父辈为基准的left值
  29. * */
  30. console.log(box.offsetTop);
  31. console.log(box.offsetLeft);
  32. console.log(box.offsetWidth)
  33. console.log(box.offsetHeight)
  34. }
  35. </script>
  36. </html>
View Code

   4.scroll系列

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style type="text/css">
  7. *{padding: 0;margin: 0;}
  8. </style>
  9. </head>
  10. <body style="width: 2000px;height: 2000px;">
  11. <div style="height: 200px;background-color: red;"></div>
  12. <div style="height: 200px;background-color: green;"></div>
  13. <div style="height: 200px;background-color: yellow;"></div>
  14. <div style="height: 200px;background-color: blue;"></div>
  15. <div style="height: 200px;background-color: gray;"></div>
  16. <div id = 'scroll' style="width: 200px;height: 200px;border: 1px solid red;overflow: auto;padding: 10px;margin: 5px 0px 0px 0px;">
  17. <p>路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城
  18. 路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城
  19. 路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城
  20. 路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城路飞学城
  21. </p>
  22. </div>
  23. </body>
  24. <script type="text/javascript">
  25. window.onload = function(){
  26. //实施监听滚动事件
  27. window.onscroll = function(){
  28. // console.log(1111)
  29. // console.log('上'+document.documentElement.scrollTop)
  30. // console.log('左'+document.documentElement.scrollLeft)
  31. // console.log('宽'+document.documentElement.scrollWidth)
  32. // console.log('高'+document.documentElement.scrollHeight)
  33. }
  34. var s = document.getElementById('scroll');
  35. s.onscroll = function(){
  36. // scrollHeight : 内容的高度+padding 不包含边框
  37. console.log('上'+s.scrollTop)
  38. console.log('左'+s.scrollLeft)
  39. console.log('宽'+s.scrollWidth)
  40. console.log('高'+s.scrollHeight)
  41. }
  42. }
  43. </script>
  44. </html>
View Code

 固定导航栏

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <style>
  7. *{
  8. padding: 0;
  9. margin:0;
  10. }
  11. .header{
  12. width: 100%;
  13. height: 80px;
  14. background-color: red;
  15. }
  16. .content{
  17. width: 100%;
  18. height: 1000px;
  19. background-color: purple;
  20. /*position: relative;*/
  21. }
  22. .fixTop{
  23. width: 100%;
  24. height: 100px;
  25. background-color: green;
  26. position: fixed;
  27. top: 0;
  28. left: 0;
  29. z-index: 1000;
  30. }
  31. .input{
  32. width: 400px;
  33. height: 60px;
  34. background-color: yellow;
  35. position: absolute;
  36. left: 50%;
  37. margin-left: -200px;
  38. top: 150px;
  39. }
  40. </style>
  41. </head>
  42. <body>
  43. <div class="header">
  44. </div>
  45.  
  46. <div class="content">
  47. <div class="input"></div>
  48. </div>
  49. <div class="fixTop" style="display: none;"></div>
  50. <script>
  51. window.onload = function() {
  52. var fromTop = document.getElementsByClassName('input')[0].offsetTop;
  53. var fixBox = document.getElementsByClassName('fixTop')[0];
  54. console.log(fromTop);
  55. var count = 0;
  56. var timer = null;
  57. window.onscroll = function() {
  58. var htmlTop = document.documentElement.scrollTop;
  59. console.log(htmlTop);
  60. if (htmlTop >= fromTop) {
  61. clearInterval(timer);
  62. timer = setInterval(function () {
  63. count+=10;
  64. fixBox.style.display = 'block';
  65. // fixBox.style.opacity = count;
  66. fixBox.style.height = count+'px';
  67. if (count == 100){
  68. console.log("11111111111111111")
  69. clearInterval(timer);
  70. count = 0
  71. }
  72. },200)
  73. }else{
  74. fixBox.style.display = 'none';
  75. }
  76. }
  77. }
  78. </script>
  79.  
  80. </body>
  81. </html>
View Code

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号