Blogger Information
Blog 16
fans 7
comment 1
visits 11505
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
12月18日- JS对象
Eric
Original
867 people have browsed it

1、JS对象(方法、属性)

定义对象、方法、属性

  1. <script>
  2. // 创建对象1
  3. var obj1 = new Object();
  4. obj1.name = 'eric';
  5. obj1.age = 20;
  6. obj.show = function(){
  7. console.log('我是obj1 的方法');
  8. };
  9. console.log(obj.name); // 输出:eric
  10. console.log(obj.show()); //输出:我是obj1 的方法
  11. // 创建对象2
  12. var obj2 = {name: "eric", age: 20};
  13. obj2.sex = '男';
  14. console.log = obj2.name; //输出:{name: "eric", age: 20, sex: "男"}
  15. // 创建对象3
  16. var obj3 = {
  17. name: 'eric',
  18. age: 20,
  19. show: function (val) {
  20. console.log(val + '我是show 方法');
  21. }
  22. };
  23. console.log(obj3.show('hello, ')); //输出:hello, 我是show 的方法
  24. //对象内部方法调用内部方法
  25. var obj4 = {
  26. name: eric,
  27. age: 20,
  28. getName = function(){
  29. console.log(obj4.name);
  30. },
  31. getInfo = function(){
  32. obj4.getName();
  33. }
  34. }
  35. console.log(obj4.getInfo()); //输出:eric
  36. </script>

延长器:setTimeout(), setInterval(), clearInterval()

setTimeout:只执行一次,setInterval:可以执行多次,clearInterval清除定时器

  1. // setTimeout
  2. setTimeout(function () {
  3. console.log('我是定时器');
  4. },3000)
  5. function show(){
  6. console.log('3秒后再执行我');
  7. }
  8. setTimeOut(show,3000);
  9. // setinterval
  10. setInterval(function(){
  11. console.log(new Date());
  12. },1000);
  13. // 设定程序4秒后停止执行
  14. var time = setInterval(function () {
  15. console.log(new Date());
  16. }, 1000);
  17. setTimeout(function () {
  18. clearInterval(time);
  19. },4000);

发送验证码小案例

  1. <button id="text">发送验证码</button>
  2. <script>
  3. var button = document.getElementById('text');
  4. button.addEventListener('click',function () {
  5. var text = document.getElementById('text').textContent;
  6. var time = 6;
  7. var val = setInterval(function () {
  8. document.getElementById('text').textContent = time+'秒后重新发送';
  9. time --;
  10. if (time == 0){
  11. clearInterval(val);
  12. document.getElementById('text').textContent = text;
  13. }
  14. },1000);
  15. });
  16. </script>

THE END !

Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post