Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jsfm] Freeze the prototype of intrenal and build-in objects #1529

Merged
merged 5 commits into from
Oct 28, 2016

Conversation

Hanks10100
Copy link
Member

@Hanks10100 Hanks10100 commented Oct 28, 2016

Freeze those objects:

  • Internal objects
    • Vm
    • App
    • Element
    • Comment
    • Listener
    • App.prototype
    • Document.prototype
    • Element.prototype
    • Comment.prototype
    • Listener.prototype
  • Build-in objects
    • Object
    • Array
    • Object.prototype
    • Array.prototype
    • String.prototype
    • Number.prototype
    • Boolean.prototype
    • Error.prototype
    • Date.prototype
    • RegExp.prototype

Once the object is frozen, there has no chance to unfreeze it.

In addition, because we use strict mode in the js bundle, modify those frozen objects will throw a TypeError.

@Jinjiang Jinjiang merged commit b4f9085 into alibaba:jsfm-feature-0.17 Oct 28, 2016
@Jinjiang Jinjiang deleted the jsfm-feature-freeze branch October 28, 2016 08:06
@Hanks10100
Copy link
Member Author

Hanks10100 commented Nov 2, 2016

Notice: Once the prototype is frozen, the instance can't override its properties.

The following code will throw a TypeError.

(function() {
  'use strict';

  function A() {}
  A.prototype.name = 'prototype';
  Object.freeze(A.prototype);

  var a = new A()
  a.name = 'instance';
})()

But you can use Object.defineProperty to override the property.

(function() {
  'use strict';

  function A() {}
  A.prototype.name = 'prototype';
  Object.freeze(A.prototype);

  var a = new A()
  Object.defineProperty(a, 'name', {
    configurable: true,
    enumerable: true,
    writable: true,
    value: 'instance'
  })
})()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants