Can I detect whether I've been given a new object as a parameter?

前端 未结 9 678
日久生厌
日久生厌 2021-01-18 01:31

Short Version

For those who don\'t have the time to read my reasoning for this question below:

Is there any way to enforce a policy of "new obje

9条回答
  •  梦谈多话
    2021-01-18 01:59

    One way to know if an object passed to a function (or a method) has been created right before the call to the function/method is that the object has a property that is initialized with the timestamp passed from a system function; in that way, looking at that property, it would be possible to resolve the problem.

    Frankly, I would not use such method because

    • I don't see any reason why the code should now if the passed parameter is an object right created, or if it has been created in a different moment.
    • The method I suggest depends from a system function that in some systems could not be present, or that could be less reliable.
    • With the modern CPUs, which are a way faster than the CPUs used 10 years ago, there could be the problem to use the right value for the threshold value to decide when an object has been freshly created, or not.

    The other solution would be to use an object property that is set to a a value from the object creator, and that is set to a different value from all the methods of the object.
    In this case the problem would be to forget to add the code to change that property in each method.

    Once again I would ask to myself "Is there a really need to do this?".

提交回复
热议问题