1. var myImage = (function(){
    2. var imgNode = document.createElement('img');
    3. document.body.appendChild(imgNode);
    4. var img = new Image();
    5. img.onload = function() {
    6. imgNode.src = img.src;
    7. }
    8. return {
    9. setSrc: function(src) {
    10. imgNode.src = './img/loading.gif';
    11. img.src = src;
    12. }
    13. }
    14. })()
    15. myImage.setSrc('https://img1.sycdn.imooc.com/5c09123400014ba418720632.jpg');
    16. // 代理模式实现
    17. var myImage = (function(){
    18. var image = document.createElement('img');
    19. document.body.appendChild(image);
    20. return {
    21. setSrc: function(src) {
    22. image.src = src;
    23. }
    24. }
    25. })();
    26. var proxyImage = (function(){
    27. var img = new Image();
    28. img.onload = function() {
    29. myImage.setSrc(this.src);
    30. }
    31. return {
    32. setSrc: function(src) {
    33. myImage.setSrc('file:///C:/Users/admin/Desktop/mask/img/7.jpg');
    34. img.src = src;
    35. }
    36. }
    37. })()
    38. proxyImage.setSrc('https://img1.sycdn.imooc.com/5c09123400014ba418720632.jpg');