es6.reflect.set.js 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. // 26.1.13 Reflect.set(target, propertyKey, V [, receiver])
  2. var dP = require('./_object-dp');
  3. var gOPD = require('./_object-gopd');
  4. var getPrototypeOf = require('./_object-gpo');
  5. var has = require('./_has');
  6. var $export = require('./_export');
  7. var createDesc = require('./_property-desc');
  8. var anObject = require('./_an-object');
  9. var isObject = require('./_is-object');
  10. function set(target, propertyKey, V /* , receiver */) {
  11. var receiver = arguments.length < 4 ? target : arguments[3];
  12. var ownDesc = gOPD.f(anObject(target), propertyKey);
  13. var existingDescriptor, proto;
  14. if (!ownDesc) {
  15. if (isObject(proto = getPrototypeOf(target))) {
  16. return set(proto, propertyKey, V, receiver);
  17. }
  18. ownDesc = createDesc(0);
  19. }
  20. if (has(ownDesc, 'value')) {
  21. if (ownDesc.writable === false || !isObject(receiver)) return false;
  22. if (existingDescriptor = gOPD.f(receiver, propertyKey)) {
  23. if (existingDescriptor.get || existingDescriptor.set || existingDescriptor.writable === false) return false;
  24. existingDescriptor.value = V;
  25. dP.f(receiver, propertyKey, existingDescriptor);
  26. } else dP.f(receiver, propertyKey, createDesc(0, V));
  27. return true;
  28. }
  29. return ownDesc.set === undefined ? false : (ownDesc.set.call(receiver, V), true);
  30. }
  31. $export($export.S, 'Reflect', { set: set });