1. S – Single responsibility principle
  2. O – Open and closed principle
  3. L – Richter substitution principle
  4. I – Interface independence principle
  5. D – Dependence inversion principle

S – Single responsibility principle a program does only one thing well. If the functionality is too complex, break it down and keep each part separate.

O – Open Closed principle open to extension, closed to modification Increasing the need to extend new code, not modify existing code is the ultimate goal of software design

L-richter substitution principle Subclasses override superclasses where superclasses can occur subclasses can occur Js is less used (weak type & inheritance is less used)

I-interface independence principle Keep the interface single and independent, avoid “fat interface” js has no interface, basically no similar to the single responsibility principle, here pays more attention to interface

D – Dependency Inversion principle Interface oriented programming, relying on abstraction rather than concrete user focus on the interface rather than concrete class implementation JS without an interface, basically useless

Use Promise to describe SO

  • The single responsibility principle: The logic in each then does one thing well
  • Open closed principle: If there are new requirements, expand then
  • Open to extension, closed to modification
function loadImg(src) {
    var promise = new Promise((reslove,reject)=>{
        var img  = document.createElement('img');
        img.onload = function() {
            reslove(img);
        }
        img.onerror = function() {
            reject('Image load failed');
        }
        img.src = src;
    })
    return promise
}
var src = 'http://www.9488.com/img/logo.png';
var result = loadImg(src)

result.then(function(img){
    console.log('img.width',img.width)    
}).then(function() {
    console.log('img.height'.img.height)
}).catch(function(err) {
    console.log(err)
})
Copy the code